Glide加载图片绘制圆角实现

Glide加载图片绘制圆角实现

// glide3.7.0版本中可以使用子类继承BitmapTransformation
	compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.makeramen:roundedimageview:2.2.1'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.facebook.fresco:fresco:0.12.0'
// 布局文件
	<ImageView
        android:id="@+id/iv_subject"
        android:gravity="center"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
// 未使用BitmapTransformation,直接通过url传入图片
	iv_subject=(ImageView)findViewById(R.id.iv_subject);
   Glide.with(this).load(url).into(iv_cardview);
// 使用BitmapTransformation
package roundimageview.forezp.com.roundimageview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
 * Created by Administrator on 2016/8/19 0019.
 */
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 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);
    }
}
// 使用transform对传入图片进行圆角封装
Glide.with(this).load(url).transform(new GlideRoundTransform(this,6)).into(iv_glide);

然后又遇到一个问题,第一次加载不到图片时之后获取到的图片会覆盖圆角。
RoundedImageView与Glide搭配使用的时候,第一次加载图片(内存中没有),后图片无法圆角,后来尝试各种改,最后想到了一个办法,就是让Glide加载图片的 时候让图不直接给控件,让控件拿到Bitmap后在操作

RoundedImageView img = (RoundedImageView) vi.findViewById(R.id.img_invitation_home);
//这里一定要加上asBitmap因为不加的话,RoundedImageView内存中第一次有该图片后无法圆角
Glide.with(ElectronicInvitationHomeActivity.this).load(urls.get(position)).asBitmap().placeholder(R.mipmap.bg_loadimage).into(img);

参考博客:https://blog.csdn.net/forezp/article/details/52249514
https://blog.csdn.net/lizhanqihd/article/details/79885660

你可能感兴趣的:(android)