android 圆角图片的实现形式

Dear 丶 2022-09-25 10:19 280阅读 0赞
  1. compile 'com.android.support:appcompat-v7:24.0.0'
  2. compile 'com.makeramen:roundedimageview:2.2.1'
  3. compile 'com.android.support:cardview-v7:24.0.0'
  4. compile 'com.github.bumptech.glide:glide:3.7.0'
  5. compile 'com.facebook.fresco:fresco:0.12.0'
  6. <android.support.v7.widget.CardView
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8. xmlns:app="http://schemas.android.com/apk/res-auto"
  9. android:id="@+id/id_cardview"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center_horizontal"
  13. app:cardBackgroundColor="@color/bg_light_gray"
  14. app:cardCornerRadius="3dp"
  15. app:cardUseCompatPadding="false"
  16. app:cardPreventCornerOverlap="true"
  17. >
  18. <ImageView
  19. android:id="@+id/iv_subject"
  20. android:gravity="center"
  21. android:scaleType="centerCrop"
  22. android:layout_width="match_parent"
  23. android:layout_height="200dp" />
  24. <TextView
  25. android:paddingLeft="5dp"
  26. android:paddingBottom="5dp"
  27. android:background="@drawable/bg_biaoti"
  28. android:id="@+id/tv_subject"
  29. android:gravity="center_vertical"
  30. android:text=""
  31. android:ellipsize="end"
  32. android:singleLine="true"
  33. android:textSize="13sp"
  34. android:textColor="@color/white"
  35. android:layout_gravity="bottom"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content" />
  38. </android.support.v7.widget.CardView>
  39. iv_round=(RoundedImageView) findViewById(R.id.iv_round);
  40. Glide.with(this).load(url).into(iv_round);
  41. iv_cardview=(ImageView)findViewById(R.id.iv_cardview);
  42. Glide.with(this).load(url).into(iv_cardview);
  43. iv_fresco=(SimpleDraweeView)findViewById(R.id.iv_fresco);
  44. Glide.with(this).load(url).into(iv_round);
  45. Glide.with(this).load(url).into(iv_cardview);
  46. Uri uri = Uri.parse(url);
  47. iv_fresco.setImageURI(uri);
  48. package roundimageview.forezp.com.roundimageview;
  49. import android.content.Context;
  50. import android.content.res.Resources;
  51. import android.graphics.Bitmap;
  52. import android.graphics.BitmapShader;
  53. import android.graphics.Canvas;
  54. import android.graphics.Paint;
  55. import android.graphics.RectF;
  56. import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
  57. import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
  58. /** * Created by Administrator on 2016/8/19 0019. */
  59. public class GlideRoundTransform extends BitmapTransformation {
  60. private static float radius = 0f;
  61. public GlideRoundTransform(Context context) {
  62. this(context, 4);
  63. }
  64. public GlideRoundTransform(Context context, int dp) {
  65. super(context);
  66. this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
  67. }
  68. @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
  69. return roundCrop(pool, toTransform);
  70. }
  71. private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
  72. if (source == null) return null;
  73. Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  74. if (result == null) {
  75. result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  76. }
  77. Canvas canvas = new Canvas(result);
  78. Paint paint = new Paint();
  79. paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
  80. paint.setAntiAlias(true);
  81. RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
  82. canvas.drawRoundRect(rectF, radius, radius, paint);
  83. return result;
  84. }
  85. @Override public String getId() {
  86. return getClass().getName() + Math.round(radius);
  87. }
  88. }
  89. Glide.with(this).load(url).transform(new GlideRoundTransform(this,6)).into(iv_glide);

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读