Glide使用 加载网络图片 圆形图片 圆角图片

时间:2025-01-27 12:49:36

在项目中总结的Glide用法(工具类)。

public class Glides {

    public static Glides instance = new Glides();

    public Glides() {
    }

    public static Glides getInstance() {
        return instance;
    }

    // 加载网络图片
    public void load(Context context, String url, ImageView imageView, GlideDrawableImageViewTarget target) {

        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .crossFade()
                .into(target);
    }

    // 加载网络图片
    public void load(Context context, Uri uri, ImageView imageView, GlideDrawableImageViewTarget target) {

        Glide.with(context)
                .load(uri)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .crossFade()
                .into(target);
    }

    // 加载网络图片
    public void load(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .crossFade()
                .into(imageView);
    }

    //加载网络或者本地图片
    public void load(Context context, Uri uri, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(uri)
                .placeholder(defaultImg)
                .error(defaultImg)
                .crossFade()
                .centerCrop()
                .into(imageView);
    }

    // 加载网络图片
    public void load(Context context, String url, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(url)
                .placeholder(defaultImg)
                .error(defaultImg)
                .crossFade()
                .centerCrop()
                .into(imageView);
    }

    // 加载网络图片
    public void loadFitCenter(Context context, String url, ImageView imageView, int defaultImg, int width, int height) {
        Glide.with(context)
                .load(url)
                .thumbnail(0.8f)
                .override(width, height)
                .dontAnimate()
                .placeholder(defaultImg)
                .error(defaultImg)
                .fitCenter()
                .into(imageView);
    }

    // 加载网络图片
    public void loadShade(Context context, String url, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(url)
                .placeholder(defaultImg)
                .error(defaultImg)
                .crossFade()
                .bitmapTransform(new ShadeTransformation(context))
                .into(imageView);
    }

    // 加载本地引导图片
    public void loadGuide(Context context, int resId, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(resId)
                .dontAnimate()
                .bitmapTransform(new CropTopTransformation(context))
                .into(imageView);
    }

    // 加载本地引导图片
    public void loadGuide(Context context, int resId, ImageView imageView, Transformation transformation) {
        Glide.with(context)
                .load(resId)
                .dontAnimate()
                .bitmapTransform(transformation)
                .into(imageView);
    }

    // 加载圆型网络图片
    public void loadCircle(Context context, String url, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(url)
                .placeholder(defaultImg)
                .error(defaultImg)
                .crossFade()
                .bitmapTransform(new CropCircleTransformation(context))
                .into(imageView);
    }

    public void loadCircleDefault(Context context, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(defaultImg)
                .placeholder(defaultImg)
                .error(defaultImg)
                .crossFade()
                .bitmapTransform(new CropCircleTransformation(context))
                .into(imageView);
    }

    public void loadCircle(Context context, Uri uri, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(uri)
                .placeholder(defaultImg)
                .error(defaultImg)
                .bitmapTransform(new CropCircleTransformation(context))
                .crossFade()
                .into(imageView);
    }

    /**
     * 圆角图片
     *
     * @param context
     * @param url
     * @param imageView
     * @param defaultImg
     */
    public void loadRound(Context context, String url, ImageView imageView, int defaultImg) {
        Glide.with(context)
                .load(url)
                .placeholder(defaultImg)
                .error(defaultImg)
                .transform(new CenterCrop(context), new GlideRoundTransform(context, 15))
                .crossFade()
                .into(imageView);
    }

    // 加载圆角网络图片
    public void loadRoundCorner(Context context, String url, ImageView imageView, int radius) {
        Glide.with(context)
                .load(url)
                .transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
                .placeholder(R.color.common_image_default)
                .error(R.color.common_image_default)
//                .crossFade()
                .into(imageView);
    }

    // 加载圆角网络图片
    public void loadRound(Context context, String url, ImageView imageView, int radius, int defaultImg) {
        Glide.with(context)
                .load(url)
                .placeholder(defaultImg)
                .error(defaultImg)
                .transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
                .crossFade()
                .into(imageView);
    }

    // 加载圆角本地图片
    public void loadRoundCorner(Context context, int resId, ImageView imageView, int radius) {
        Glide.with(context)
                .load(resId)
                .transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
                .into(imageView);
    }


    // 加载本地图片
    public void load(Context context, int resid, ImageView imageView) {
        Glide.with(context)
                .load(resid)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .crossFade()
                .into(imageView);
    }

    // 加载本地图片
    public void loadDecor(Context context, String url, ImageView imageView, int errresid, int width, int height) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .priority(Priority.IMMEDIATE)
                .dontAnimate()
                .thumbnail(0.1f)
                .override(width, height)
                .into(imageView);
    }

    // 加载天空网络图片,不用设高
    public void loadDecor(Context context, String url, ImageView imageView, int errresid) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .priority(Priority.IMMEDIATE)
                .dontAnimate()
                .into(imageView);
    }


    // 加载本地图片,错误或占位用透明
    public void loadDecor(Context context, int resid, ImageView imageView, int errresid, int width, int height) {
        Glide.with(context)
                .load(resid)
                .asBitmap()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .priority(Priority.IMMEDIATE)
                .dontAnimate()
                .thumbnail(0.1f)
                .override(width, height)
                .into(imageView);
    }


    // 加载天空图片,错误或占位用透明
    public void loadDecor(Context context, int resid, ImageView imageView, int errresid) {
        Glide.with(context)
                .load(resid)
                .asBitmap()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .priority(Priority.IMMEDIATE)
                .dontAnimate()
                .into(imageView);
    }

    public void loadNoAnim(Context context, String uri, ImageView imageView, int placeholder) {

        Glide.with(context)
                .load(Uri.parse(uri))
                .placeholder(placeholder)
                .error(placeholder)
                .dontAnimate()//color filter error
                .into(imageView);
    }

    // 加载圆型本地图片
    public void loadCircle(Context context, int resId, ImageView imageView) {
        Glide.with(context)
                .load(resId)
                .placeholder(R.color.trans)
                .error(R.color.trans)
                .transform(new GlideCircleTransform(context))
                .crossFade()
                .into(imageView);
    }

    // 加载圆型网络图片
    public void loadCircle(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.color.trans)
                .error(R.color.trans)
                .transform(new GlideCircleTransform(context))
                .crossFade()
                .into(imageView);
    }

    // 加载网络图片动画
    public void loadAnima(Context context, String url, Animation animation, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .animate(animation)
                .crossFade()
                .into(imageView);
    }

    // 加载网络图片动画
    public void loadAnima(Context context, String url, int animationId, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .animate(animationId)
                .crossFade()
                .into(imageView);
    }

    // 加载本地图片动画
    public void loadAnima(Context context, int resId, Animation animation, ImageView imageView) {
        Glide.with(context)
                .load(resId)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .animate(animation)
                .crossFade()
                .into(imageView);
    }


    // 加载drawable图片
    public void loadAnima(Context context, int resId, int animationId, ImageView imageView) {
        Glide.with(context)
                .load(resId)
                .placeholder(R.color.abc_tab_text_normal)
                .error(R.color.abc_tab_text_normal)
                .animate(animationId)
                .crossFade()
                .into(imageView);
    }


}
public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

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

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = ().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);
    }
}
public class CropTopTransformation implements Transformation<Bitmap> {

    private Context mContext;
    private BitmapPool mBitmapPool;
    private int mWidth;
    private int mHeight;

    private  mCropType = ;

    public CropTopTransformation(Context context) {
        this(Glide.get(context).getBitmapPool());
        mContext = context;
    }

    public CropTopTransformation(BitmapPool pool) {
        this.mBitmapPool = pool;
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {


        Bitmap source = resource.get();
        float sWidth = (float) ();
        float sHeight = (float) ();

        float wScale = outWidth / sWidth;

        float calculateHeight = wScale * sHeight + 0.5f;

        Bitmap fitBitmap = (source, mBitmapPool, outWidth, (int) calculateHeight);

        int fWidth = ();
        int fHeight = ();

        Bitmap bitmap = mBitmapPool.get(outWidth, outHeight, .ARGB_8888);
        if (bitmap == null) {
            bitmap = (outWidth, outHeight, .ARGB_8888);
        }

        Matrix matrix = new Matrix();
        if (fHeight > outHeight) {
            (0, fHeight - outHeight);
        } else {
            (0, outHeight - fHeight);
        }

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();

        (true);
        (true);

        (fitBitmap, matrix, paint);

        return (bitmap, mBitmapPool);
    }

    @Override
    public String getId() {
        return "CropCircleTransformation()";
    }

    private float getTop(float scaledHeight) {
        switch (mCropType) {
            case TOP:
                return 0;
            case CENTER:
                return (mHeight - scaledHeight) / 2;
            case BOTTOM:
                return mHeight - scaledHeight;
            default:
                return 0;
        }
    }
}
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;

        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();
    }
}
public class ShadeTransformation implements Transformation<Bitmap> {

    private Context mContext;
    private BitmapPool mBitmapPool;
    private int mWidth;
    private int mHeight;

    private  mCropType = ;

    public ShadeTransformation(Context context) {
        this(Glide.get(context).getBitmapPool());
        mContext = context;
    }

    public ShadeTransformation(BitmapPool pool) {
        this.mBitmapPool = pool;
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {


        Bitmap source = resource.get();

        Bitmap bitmap = mBitmapPool.get(outWidth, outHeight, .ARGB_8888);
        if (bitmap == null) {
            bitmap = (outWidth, outHeight, .ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();

        (true);
        (true);

        ((bitmap, source, outWidth, outHeight), null, new Rect(0, 0, outWidth, outHeight), paint);

        GradientDrawable drawable =
                new GradientDrawable(.TOP_BOTTOM, new int[]{("#80000000"), ("#00000000")});

        (0, 0, outWidth, outHeight);

        (canvas);

        return (bitmap, mBitmapPool);
    }

    @Override
    public String getId() {
        return "CropCircleTransformation()";
    }

    private float getTop(float scaledHeight) {
        switch (mCropType) {
            case TOP:
                return 0;
            case CENTER:
                return (mHeight - scaledHeight) / 2;
            case BOTTOM:
                return mHeight - scaledHeight;
            default:
                return 0;
        }
    }
}