Android 必知必會 - Glide 加載圓形圖片和圓角圖片的兩種方法

shellqqq 8年前發布 | 101K 次閱讀 Bitmap 安卓開發 Android開發 移動開發

兩種使用 BitmapTransformation 來實現 Glide 加載圓形圖片和圓角圖片的方法。

背景

Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 來進行處理。

Round Pictures: CircleImageView / CircularImageView / RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue.

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

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

PS: RoundedBitmapDrawable 是 support.v4 下的一個類,想了解更多,可以閱讀我之前的文章: Android 必知必會-使用 supportV4 的 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 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();
 */

實現圓形:

RoundedBitmapDrawable drawableA = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        drawableA.setCircular(true);
        Bitmap a = drawableToBitmap(drawableA);

實現圓角:

RoundedBitmapDrawable drawableB = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
       drawableB.setCornerRadius(30L);
       Bitmap b = drawableToBitmap(drawableB);

 

來自:http://likfe.com/2016/08/31/android-glide-roundedBitmapTransformation/

 

 本文由用戶 shellqqq 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!