使用Glide对网络图片进行圆形和圆角的处理

在开发中欧经常会遇见对图片的圆形和圆角的处理(头像一般圆形较多),之前使用的还是Volley-ImageLoader来进行的加载网络图片,当时遇见这个需求找了许多资料,后来朋友一致推荐我将Volley-ImageLoader换成Glide,不仅可以对网络图片进行这些处理最主要还是Google出的哦!对自己的亲儿子Google肯定是会维护下去的,所以鉴于此我就将其换成了Glide,换了之后才发现还是比较好用的。。。好了废话不多说直接上代码。。


对图片的圆形处理::

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;

        // TODO this could be acquired from the pool too
        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.with(mContext).load(url)
        .transform(new GlideCircleTransform(mContext))
        .into((ImageView) getView(viewId));

这里就不解释了非常一目了然。。。


下面贴出图片圆角处理的代码::


public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
                               int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private 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);
    }
}

使用同样灰常简单::

Glide.with(mContext).load(url)
        .transform(new GlideCircleTransform(mContext,10))
        .into((ImageView) getView(viewId));
 

说明:

   无论是圆形还是圆角这里into()中的东西都是展示的控件,圆角中的10是圆角的弧度。。。


至此圆形和圆角处理就结束了。。。很简单没什么要解释的。。。








你可能感兴趣的:(使用Glide对网络图片进行圆形和圆角的处理)