最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼

 更新時間:2017年05月31日 15:18:38   作者:陪你嘮嗑  
本篇文章主要介紹了Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼,具體一定的參考價值,有興趣的可以了解一下

一、簡介:

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

二、網(wǎng)上的實(shí)現(xiàn)方式

這里介紹下網(wǎng)上常見的方式和使用 RoundedBitmapDrawable 兩種方法,本質(zhì)上是差不多的:

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

實(shí)現(xiàn)圓形圖片:

/**
 * 
 * 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();
  }
}

實(shí)現(xiàn)圓角圖片:

/**
 * Glide 圓角 Transform
 */

public class GlideRoundTransform extends BitmapTransformation {

  private static float radius = 0f;

  /**
 * 構(gòu)造函數(shù) 默認(rèn)圓角半徑 4dp
 *
 * @param context Context
 */
  public GlideRoundTransform(Context context) {
    this(context, 4);
  }

  /**
 * 構(gòu)造函數(shù)
 *
 * @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);
  }
}

三、筆者比較喜歡的簡便的實(shí)現(xiàn)方式

//加載圓角圖片
   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) //設(shè)置緩存
         .into(new BitmapImageViewTarget(imageView){
           @Override
           protected void setResource(Bitmap resource) {
             super.setResource(resource);
             RoundedBitmapDrawable circularBitmapDrawable =
                 RoundedBitmapDrawableFactory.create(context.getResources(), resource);
             circularBitmapDrawable.setCornerRadius(10); //設(shè)置圓角弧度
             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) //設(shè)置緩存
        .into(new BitmapImageViewTarget(imageView) {
          @Override
          protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
          }
        });

  }

關(guān)于drawableToBitmap的源碼的實(shí)現(xiàn)是這樣的

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;
    // 建立對應(yīng) bitmap
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 建立對應(yīng) bitmap 的畫布
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    // 把 drawable 內(nèi)容畫到畫布中
    drawable.draw(canvas);
    return bitmap;
  }
/**
 * RoundedBitmapDrawable 是 V4 下的一個類,不能簡單的通過:強(qiáng)制轉(zhuǎn)換成 BitmapDrawable
 * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
 */

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

资中县| 广南县| 离岛区| 岚皋县| 胶南市| 保定市| 肃宁县| 华容县| 米泉市| 曲阳县| 永济市| 军事| 松溪县| 大方县| 辽宁省| 柳林县| 旬邑县| 历史| 白玉县| 桃园县| 濉溪县| 香河县| 天全县| 响水县| 梅河口市| 武夷山市| 大兴区| 宜良县| 涡阳县| 合水县| 盐津县| 昔阳县| 广东省| 包头市| 济南市| 宜宾市| 庐江县| 安乡县| 永福县| 宣城市| 民县|