欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Glide 显示圆形头像

程序员文章站 2022-03-27 11:34:27
1.事例代码ImageLoader.displayCircle(iv_head, data.getHeadimgurl(), false, ctx);2.工具类代码import android.app.Activity;import android.content.Context;import android.os.Build.VERSION;import android.os.Build.VERSION_CODES;import android.view.View;impo...

1.事例代码

ImageLoader.displayCircle(iv_head, data.getHeadimgurl(), false, ctx);

2.工具类代码



import android.app.Activity;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by codeest on 2016/8/2.
 */
public class ImageLoader {
    
    public static void load(Context context, String url, ImageView iv) { //使用Glide加载圆形ImageView(如头像)时,不要使用占位图
        Glide.with(context).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
    }
    
    /**
     * 加载本地图片
     * @param context
     * @param resId
     * @param imageView
     */
    public static void loadLocal(Context context, int resId, ImageView imageView) {
        Glide.with(context).load(resId).into(imageView);
    }
    
    public static void load(Context context, String url, ImageView iv, @DrawableRes int placeholder) { //使用Glide加载圆形ImageView(如头像)时,不要使用占位图
        Glide.with(context)
                .load(url)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(placeholder)
                .into(iv);
    }
    
    public static void load(Activity activity, String url, ImageView iv) { //使用Glide加载圆形ImageView(如头像)时,不要使用占位图
        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
            if (!activity.isDestroyed()) {
                Glide.with(activity).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
            }
        }
        else {
            Glide.with(activity).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
        }
    }
    
    public static void loadAll(Context context, String url, ImageView iv) { //不缓存,全部从网络加载
        Glide.with(context)
                .load(url)
                .crossFade()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(iv);
    }
    
    public static void loadAll(Activity activity, String url, ImageView iv) { //不缓存,全部从网络加载
        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
            if (!activity.isDestroyed()) {
                Glide.with(activity)
                        .load(url)
                        .crossFade()
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(iv);
            }
        }
        else {
            Glide.with(activity)
                    .load(url)
                    .crossFade()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(iv);
        }
    }
    
    public static void clear(Context context, View view) {
        Glide.clear(view);
    }


    /**
     * 展示圆形图片
     * @param imageView
     * @param uri
     * @param cache 缓存
     */
    public static void displayCircle(ImageView imageView, String uri,boolean cache,Context ctx){
        try {
            if (imageView != null) {
                Glide.with(ctx).load(uri)
                        .skipMemoryCache(cache?false:true)
                        .diskCacheStrategy(cache?DiskCacheStrategy.RESULT:DiskCacheStrategy.NONE)
                        .bitmapTransform(new CropCircleTransformation(ctx))
                        .error(R.color.gray)
                        .crossFade()
                        .thumbnail(0.5f)
                        .priority(Priority.HIGH)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 展示圆形图片
     * @param imageView
     * @param uri
     * @param cache 缓存
     */
    public static void displayCircle(ImageView imageView, @DrawableRes int uri, boolean cache,Context ctx){
        try {
            if (imageView != null) {
                Glide.with(ctx).load(uri)
                        .skipMemoryCache(cache?false:true)
                        .diskCacheStrategy(cache?DiskCacheStrategy.RESULT:DiskCacheStrategy.NONE)
                        .bitmapTransform(new CropCircleTransformation(ctx))
                        .error(R.color.colorPrimary)
                        .crossFade()
                        .thumbnail(0.5f)
                        .priority(Priority.HIGH)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

3.添加编译的库 

api 'jp.wasabeef:glide-transformations:2.0.1'
    api 'com.github.bumptech.glide:glide:3.7.0'

 

本文地址:https://blog.csdn.net/lsw8569013/article/details/108116235

相关标签: Glide android