首页 > 代码库 > Glide4.0使用

Glide4.0使用

导入

  1. dependencies {
      compile ‘com.github.bumptech.glide:glide:4.0.0‘
      compile ‘com.android.support:support-v4:25.3.1‘
      annotationProcessor ‘com.github.bumptech.glide:compiler:4.0.0‘
    }

     

  2. 2.下载jar  地址:glide.jar

基本使用

  1. 基本使用
Glide.with(this).load(imgUrl).into(img1);

  2. 设置预加载和加载失败的图片及缓存策略

  

RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher)//预加载图片
.error(R.mipmap.ic_launcher)//加载失败显示图片
.priority(Priority.HIGH)//优先级
.diskCacheStrategy(DiskCacheStrategy.NONE);//缓存策略
Glide.with(this).load(imgUrl).apply(options).into(img1);

 

 

显示圆形图片

  •   定义转化圆形的类 继承自BitmapTransforation

  

public class GlideCircleTransform extends BitmapTransformation {
    public GlideCircleTransform(){
        super();
    }

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

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

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

 

 

  •  在RequestOptions中配置该类   

  

RequestOptions options2 = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)//预加载图片
                        .error(R.mipmap.ic_launcher)//加载失败显示图片
                        .priority(Priority.HIGH)//优先级
                        .diskCacheStrategy(DiskCacheStrategy.NONE)//缓存策略
                        .transform(new GlideCircleTransform());//转化为圆形
Glide.with(this).load(imgUrl).apply(options2).into(img1);

 

 

 

显示圆角图片

  • 定义转化圆形的类 继承自BitmapTransforation

  

public class GlideRoundTransform extends BitmapTransformation {

    private static float radius=0f;
    public GlideRoundTransform(){
        this(4);
    }
    public GlideRoundTransform(int dp){
        super();
        this.radius= Resources.getSystem().getDisplayMetrics().density*dp;
    }


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

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }


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

 

  • 在RequestOptions中配置该类 

 

RequestOptions options1 = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)//预加载图片
                        .error(R.mipmap.ic_launcher)//加载失败显示图片
                        .priority(Priority.HIGH)//优先级
                        .diskCacheStrategy(DiskCacheStrategy.NONE)//缓存策略
                        .transform(new GlideRoundTransform(20));//转化为圆角
Glide.with(this).load(imgUrl).apply(options1).into(img1);

 

技术分享

 

Glide4.0使用