国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中Glide加載圓形圖片和圓角圖片實例代碼

2019-12-12 02:46:57
字體:
來源:轉載
供稿:網友

一、簡介:

介紹兩種使用 BitmapTransformation 來實現 Glide 加載圓形圖片和圓角圖片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 來進行處理。

二、網上的實現方式

這里介紹下網上常見的方式和使用 RoundedBitmapDrawable 兩種方法,本質上是差不多的:

  1. 使用 Canvas 和 Paint 來繪制
  2. 使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable

實現圓形圖片:

/** *  * Glide 圓形圖片 Transform */public class GlideCircleTransform extends BitmapTransformation {  public GlideCircleTransform(Context context) {    super(context);  }  @Override  protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {    return circleCrop(pool, toTransform);  }  private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {    if (source == null) return null;    int size = Math.min(source.getWidth(), source.getHeight());    int x = (source.getWidth() - size) / 2;    int y = (source.getHeight() - size) / 2;    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);    if (result == null) {      result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);    }    Canvas canvas = new Canvas(result);    Paint paint = new Paint();    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));    paint.setAntiAlias(true);    float r = size / 2f;    canvas.drawCircle(r, r, r, paint);    return result;  }  @Override  public String getId() {    return getClass().getName();  }}

實現圓角圖片:

/** * Glide 圓角 Transform */public class GlideRoundTransform extends BitmapTransformation {  private static float radius = 0f;  /** * 構造函數 默認圓角半徑 4dp * * @param context Context */  public GlideRoundTransform(Context context) {    this(context, 4);  }  /** * 構造函數 * * @param context Context * @param dp 圓角半徑 */  public GlideRoundTransform(Context context, int dp) {    super(context);    radius = Resources.getSystem().getDisplayMetrics().density * dp;  }  @Override  protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {    return roundCrop(pool, toTransform);  }  private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {    if (source == null) return null;    Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);    if (result == null) {      result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);    }    Canvas canvas = new Canvas(result);    Paint paint = new Paint();    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));    paint.setAntiAlias(true);    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());    canvas.drawRoundRect(rectF, radius, radius, paint);    return result;  }  @Override  public String getId() {    return getClass().getName() + Math.round(radius);  }}

三、筆者比較喜歡的簡便的實現方式

//加載圓角圖片   public static void loadRoundImage(final Context context, String url,final ImageView imageView){     Glide.with(context)         .load(url)         .asBitmap()         .placeholder(placeholder)         .error(placeholder)         .diskCacheStrategy(DiskCacheStrategy.ALL) //設置緩存         .into(new BitmapImageViewTarget(imageView){           @Override           protected void setResource(Bitmap resource) {             super.setResource(resource);             RoundedBitmapDrawable circularBitmapDrawable =                 RoundedBitmapDrawableFactory.create(context.getResources(), resource);             circularBitmapDrawable.setCornerRadius(10); //設置圓角弧度             imageView.setImageDrawable(circularBitmapDrawable);           }         });   }  //加載圓形圖片  public static void loadCirclePic(final Context context, String url, final ImageView imageView) {    Glide.with(context)        .load(url)        .asBitmap()        .placeholder(placeholder)        .error(placeholder)        .diskCacheStrategy(DiskCacheStrategy.ALL) //設置緩存        .into(new BitmapImageViewTarget(imageView) {          @Override          protected void setResource(Bitmap resource) {            RoundedBitmapDrawable circularBitmapDrawable =                RoundedBitmapDrawableFactory.create(context.getResources(), resource);            circularBitmapDrawable.setCircular(true);            imageView.setImageDrawable(circularBitmapDrawable);          }        });  }

關于drawableToBitmap的源碼的實現是這樣的

public static Bitmap drawableToBitmap(Drawable drawable) {    // 取 drawable 的長寬    int w = drawable.getIntrinsicWidth();    int h = drawable.getIntrinsicHeight();    // 取 drawable 的顏色格式    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888        : Bitmap.Config.RGB_565;    // 建立對應 bitmap    Bitmap bitmap = Bitmap.createBitmap(w, h, config);    // 建立對應 bitmap 的畫布    Canvas canvas = new Canvas(bitmap);    drawable.setBounds(0, 0, w, h);    // 把 drawable 內容畫到畫布中    drawable.draw(canvas);    return bitmap;  }/** * RoundedBitmapDrawable 是 V4 下的一個類,不能簡單的通過:強制轉換成 BitmapDrawable * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap(); */

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孝感市| 保靖县| 藁城市| 都兰县| 普洱| 宁国市| 丰原市| 铅山县| 秭归县| 大冶市| 宁化县| 玉田县| 娄底市| 监利县| 策勒县| 韶山市| 兴国县| 开鲁县| 北流市| 博乐市| 阿鲁科尔沁旗| 襄樊市| 清新县| 无为县| 崇州市| 称多县| 平塘县| 九龙坡区| 岐山县| 无为县| 江山市| 北票市| 集安市| 乐亭县| 军事| 襄垣县| 连南| 澜沧| 安达市| 平利县| 吕梁市|