Glide封装

添加依赖
compile ‘com.github.bumptech.glide:glide:3.6.1’//显示图片框架

Glide工具类的封装

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.jaydenxiao.common.R;

import java.io.File;

/**

  • Description : 图片加载工具类 使用glide框架封装
    */
    public class ImageLoaderUtils {

    public static void display(Context context, ImageView imageView, String url, int placeholder, int error) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url).placeholder(placeholder)
    .error(error).crossFade().into(imageView);
    }

    public static void display(Context context, ImageView imageView, String url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .centerCrop()
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .crossFade().into(imageView);
    }

    public static void display(Context context, ImageView imageView, File url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .centerCrop()
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .crossFade().into(imageView);
    }
    public static void displaySmallPhoto(Context context, ImageView imageView, String url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url).asBitmap()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .thumbnail(0.5f)
    .into(imageView);
    }
    public static void displayBigPhoto(Context context, ImageView imageView, String url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url).asBitmap()
    .format(DecodeFormat.PREFER_ARGB_8888)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .into(imageView);
    }
    public static void display(Context context, ImageView imageView, int url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .centerCrop()
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .crossFade().into(imageView);
    }
    public static void displayRound(Context context,ImageView imageView, String url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .transform(new CenterCrop(context),new GlideRoundTranform(context))//看centerCrop()方法的源码可知,
    // 也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果,解决办法:在transform中new CenterCrop(context)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .crossFade()
    .into(imageView);
    }
    public static void displayRound(Context context,ImageView imageView, String url,int dp) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .transform(new CenterCrop(context),new GlideRoundTranform(context,dp))//看centerCrop()方法的源码可知,
    // 也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果,解决办法:在transform中new CenterCrop(context)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.ic_empty_picture)
    .crossFade()
    .into(imageView);
    }
    public static void displayCircle(Context context,ImageView imageView, String url) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.toux2)
    .centerCrop().transform(new GlideCircleTransfromUtil(context)).into(imageView);
    }
    public static void displayCircle(Context context,ImageView imageView, int resId) {
    if (imageView == null) {
    throw new IllegalArgumentException(“argument error”);
    }
    Glide.with(context).load(resId)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .placeholder(R.drawable.ic_image_loading)
    .error(R.drawable.toux2)
    .centerCrop().transform(new GlideCircleTransfromUtil(context)).into(imageView);
    }

}

Tranform部分

圆角的transform

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 user on 2018/5/30.
    */

public class GlideRoundTranform extends BitmapTransformation {

private static float radius = 0f;

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

public GlideRoundTranform(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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

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

/**

  • description:glide转圆形图片

  • Created by xsf

  • on 2016.04.15:26
    */
    public class GlideCircleTransfromUtil extends BitmapTransformation {
    public GlideCircleTransfromUtil(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();
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值