Glide图片处理框架(含圆角哦)

时间:2025-01-27 12:50:26
public  class  TestGlideActivity  extends  Activity {
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(.activity_test);
         ImageView targetView = (ImageView) findViewById(.iv_target);
         //缩略图请求
         DrawableRequestBuilder<String> thumbnailRequest = Glide
                 .with( this )
                 .load( "http:///uploads/allimg/141106/" );
 
         ( this ).
                 load().
//                placeholder(.bg_loading).//加载中显示的图片
//                error(.bg_error).//加载失败时显示的图片
//                crossFade(1000).//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
                 override( 80 80 ). //设置最终显示的图片像素为80*80,注意:这个是像素,而不是控件的宽高
                 centerCrop(). //中心裁剪,缩放填充至整个ImageView
                 skipMemoryCache( true ). //跳过内存缓存
                 diskCacheStrategy(). //保存最终图片
                 thumbnail(thumbnailRequest). //设置缩略图
                 into(targetView);
     }

15,设置圆角


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 = ((), ());
        int x = (() - size) / 2;
        int y = (() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = (source, x, y, size, size);

        Bitmap result = (size, size, .ARGB_8888);
        if (result == null) {
            result = (size, size, .ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        (new BitmapShader(squared, , ));
        (true);
        float r = size / 2f;
        (r, r, r, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName();
    }
}

自定义一个extend BitmapTransformation的方法,把获得的bitmap转化成圆形图片,下面是使用方法
private RequestManager glideRequest;
glideRequest = (this);
("/img/").transform(new GlideCircleTransform(context)).into(imageView);

这里不得不强调下Glide的一个强大的功能,当你在With后面的传Activity或者Fragment,Glide就可以根据当前Activity或者Fragment的生命周期维护图片的生命周期,比如但activity销毁的时候,就会自动取消需要加载的图片  
下载图片转换层圆角图片的方法
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);
         = ().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 = ((), (), .ARGB_8888);
        if (result == null) {
            result = ((), (), .ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        (new BitmapShader(source, , ));
        (true);
        RectF rectF = new RectF(0f, 0f, (), ());
        (rectF, radius, radius, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + (radius);
    }
}

在这个方法里面,你可以自定义圆角的大小,使用方式也非常简单
("/img/").transform(new GlideRoundTransform(context)).into(imageView);

("/img/").transform(new GlideRoundTransform(context, 10)).into(imageView);