欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、簡介:

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

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

相關(guān)文章

  • Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例

    Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例

    對(duì)于不同的屏幕密度、不同的設(shè)備方向,不同的語言和區(qū)域,都會(huì)涉及到備選 drawable 資源,下面這篇文章主要給你大家介紹了關(guān)于Android自定義Drawable之在Drawable中部指定透明區(qū)域的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評(píng)分示例

    Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評(píng)分示例

    本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評(píng)分示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-02-02
  • mui,h5+中實(shí)現(xiàn)控制頁面load顯示

    mui,h5+中實(shí)現(xiàn)控制頁面load顯示

    這篇文章主要介紹了mui,h5+中實(shí)現(xiàn)控制頁面load顯示的相關(guān)代碼寫法和運(yùn)用技巧,需要的朋友參考一下。
    2017-11-11
  • Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Android Studio配置本地SDK的方法

    Android Studio配置本地SDK的方法

    這篇文章主要介紹了Android Studio配置本地SDK的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android開發(fā)之禁止下拉通知欄的方法

    Android開發(fā)之禁止下拉通知欄的方法

    這篇文章主要介紹了Android開發(fā)之禁止下拉通知欄的方法,實(shí)例分析了Android權(quán)限控制與Activity相應(yīng)設(shè)置技巧,需要的朋友可以參考下
    2016-01-01
  • Android調(diào)用相機(jī)并將照片存儲(chǔ)到sd卡上實(shí)現(xiàn)方法

    Android調(diào)用相機(jī)并將照片存儲(chǔ)到sd卡上實(shí)現(xiàn)方法

    Android中實(shí)現(xiàn)拍照有兩種方法,一種是調(diào)用系統(tǒng)自帶的相機(jī),還有一種是自己用Camera類和其他相關(guān)類實(shí)現(xiàn)相機(jī)功能,這種方法定制度比較高,需要的朋友可以了解下
    2012-12-12
  • Android監(jiān)聽ScrollView滑動(dòng)距離的簡單處理

    Android監(jiān)聽ScrollView滑動(dòng)距離的簡單處理

    這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽ScrollView滑動(dòng)距離的簡單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Android貝塞爾曲線實(shí)現(xiàn)手指軌跡

    Android貝塞爾曲線實(shí)現(xiàn)手指軌跡

    這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)手指軌跡效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效

    Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07

最新評(píng)論