当前位置:网站首页>Simple encapsulation of glide tool class

Simple encapsulation of glide tool class

2022-08-09 13:15:00 Xie Dong_

    开题:Actually to request frame images,I have always admired and skills in the use of a good,As early as I had for long beforeUniversal-image-loaderWritten technology sharing blog,With its more mature,And more stable to use,So when doing project usually preferredUniversal-image-loader,But for it if not in the project plan not packaging,It is easy to cause code redundancy,Just recently in writing for the new project,So going to try a few more famous pictures other loading framework,After do a contrast,决定使用Glid.

       This blog will start simple introduceGlide的简单使用,I will use through a few simpleGlideThe scenario to introduceGlide,Then summarize some for youGlide常用的方法,At the end of the blog I will post in my project encapsulated tools for your reference.关于Glide的优缺点对比,我就不在此赘述了,网上比比皆是,The purpose of this blog is to tell you a piece of the use of image again request frame encapsulation,More convenient we developed using.

一、Glide使用场景分析

1.使用GlideThe basic method for the image

 

 /**
     * Simple get photo
     *
     * @param path
     * @param view
     */
    public static void getImage(String path, ImageView view) {
        Glide.with(context)
                .load(path)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(view);
    }

2.The size of the load in the loaded pictures

 

 /**
     * According to the specified size loading pictures
     *
     * @param path
     * @param width
     * @param height
     * @param imageView
     */
    public static void getImageBySize(String path, int width, int height, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .override(width, height)
                .into(imageView);
    }


3.Set loading picturesanimmation动画

 /**
     * Animate the loading pictures
     *
     * @param path
     * @param imageView
     * @param anim
     */
    public static void getImageWithAnim(String path, ImageView imageView, int anim) {
        Glide.with(context)
                .load(path)
                .animate(anim)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .into(imageView);
    }


4.Even the exciting loadGif,Is simply a few lines of code to accomplish,你会发现原来glideThe image is so elegant

/**
     * 加载动态Gif
     *
     * @param path
     * @param imageView
     */
    public static void getImageByDynamicGif(String path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .asGif()
                .centerCrop()
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(imageView);
    }

 

5.Other functions request frame imagesGlide毫不逊色,使用GlideSet the listener when loading pictures

/**
     * Take the listener loading pictures
     *
     * @param path
     * @param imageView
     * @param requstlistener
     */
    public static void getImageWithListener(String path, ImageView imageView, RequestListener<String, GlideDrawable> requstlistener) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .listener(requstlistener)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(imageView);
    }

 

Simple combing the use of a few common scenarios for you,More method signature I would put a I finally based on the packagingGlideUtilUtility class for your reference.

 

二、关于Glide的常用方法

 

1.thumbnail(float sizeMultiplier). Request a given coefficient thumbnails.If the thumbnail before full size chart after loading,According to shrink
  略图,否则就不显示.系数sizeMultiplier必须在(0,1)之间,Can recursive calls the method.
  2.sizeMultiplier(float sizeMultiplier). Before loading resources toTargetSize set coefficient.
  3.diskCacheStrategy(DiskCacheStrategy strategy).设置缓存策略.DiskCacheStrategy.SOURCE:缓存
  原始数据,DiskCacheStrategy.RESULT:Cache transform(如缩放、裁剪等)After the resource data,
  DiskCacheStrategy.NONE:什么都不缓存,DiskCacheStrategy.ALL:缓存SOURC和RESULT.
  默认采用DiskCacheStrategy.RESULT策略,对于download only操作要使用DiskCacheStrategy.SOURCE.
  4.priority(Priority priority).     指定加载的优先级,优先级越高越优先加载,But does not guarantee that all pictures
  All sequential loading.枚举Priority.IMMEDIATE,Priority.HIGH,
  Priority.NORMAL,Priority.LOW.默认为Priority.NORMAL.
  5.dontAnimate(). Remove all the animation.
  6.animate(int animationId). In asynchronous loading resources to complete will perform the animation.
  7.animate(ViewPropertyAnimation.Animator animator). In asynchronous loading resources to complete will perform the animation.
  8.placeholder(int resourceId).    Set the resource in the process of loading placeholderDrawable.
  9.placeholder(Drawable drawable). Set the resource in the process of loading placeholderDrawable.
  10.fallback(int resourceId).      设置modelIs empty to showDrawable.如果没设置fallback,
  modelWhen null will showerror的Drawable,如果error的Drawable也没设置,
  就显示placeholder的Drawable.
  11.fallback(Drawable drawable).设置modelIs empty, displayDrawable.
  12.error(int resourceId).设置loadDisplayed when failureDrawable.
  13.error(Drawable drawable).设置loadDisplayed when failureDrawable.
  14.listener(RequestListener<? super ModelType, TranscodeType> requestListener). Monitor resources to add
 Load state of the request,You can use two callback:onResourceReady(R resource, T model,
 Target<R> target, boolean isFromMemoryCache, boolean isFirstResource)
 和onException(Exception e, T model, Target<R> target, boolean isFirstResource)
 But not every request using the new listener,To avoid unnecessary memory application,Can use the singleton unified exception monitoring and processing.
 15.skipMemoryCache(boolean skip). Set whether to skip the memory cache,But does not guarantee that must not be cached
 (Such as request haven't set in the load resources and skip the memory cache,The resources will be cached in memory).
 16.override(int width, int height). 重新设置Target的宽高值(单位为pixel).
 17.into(Y target).Set of resources will be loaded into theTarget.
 18.into(ImageView view).   Set of resources will be loaded into theImageView.取消该ImageViewBefore all of the loading and release resources.
 19.into(int width, int height).     Background threads load to load resources wide high value(单位为pixel).
 20.preload(int width, int height).  预加载resource到缓存中(单位为pixel).
 21.asBitmap(). Whether the resource isgif动画,都作为Bitmap对待.如果是gifThe animation will stop in the first frame.
 22.asGif().The resources asGifDrawable对待.如果资源不是gifThe animation will fail,会回调.error().

三、基于GlideSimple packaging tool class

 

/**
 * Glide简单封装
 * Created by 谢栋 on 2017/10/26.
 */

public class GlideUtil {

    private static Context context = AppManage.getInstance().currentActivity();

    /**
     * Simple get photo
     *
     * @param path
     * @param view
     */
    public static void getImage(String path, ImageView view) {
        Glide.with(context)
                .load(path)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(view);
    }

    /**
     * According to the specified size loading pictures
     *
     * @param path
     * @param width
     * @param height
     * @param imageView
     */
    public static void getImageBySize(String path, int width, int height, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .override(width, height)
                .into(imageView);
    }

    /**
     * Specified load waiting with loading failure pictures
     *
     * @param path
     * @param imageView
     * @param errorRes
     * @param loadingRes
     */
    public static void getImageWithErrorLoadingImg(String path, ImageView imageView, int errorRes, int loadingRes) {
        Glide.with(context)
                .load(path)
                .placeholder(loadingRes)
                .error(errorRes)
                .into(imageView);
    }


    /**
     * Whether to skip loading
     *
     * @param path
     * @param view
     * @param isSkipMemoryCache
     */
    public static void getImage(String path, ImageView view, boolean isSkipMemoryCache) {
        Glide.with(context)
                .load(path)
                .skipMemoryCache(isSkipMemoryCache)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .into(view);
    }

    /**
     * Animate the loading pictures
     *
     * @param path
     * @param imageView
     * @param anim
     */
    public static void getImageWithAnim(String path, ImageView imageView, int anim) {
        Glide.with(context)
                .load(path)
                .animate(anim)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .into(imageView);
    }

    /**
     * 加载动态Gif
     *
     * @param path
     * @param imageView
     */
    public static void getImageByDynamicGif(String path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .asGif()
                .centerCrop()
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(imageView);
    }

    /**
     * 加载静态Gif
     *
     * @param path
     * @param imageView
     */
    public static void getImageByStaticGif(String path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .asBitmap()
                .centerCrop()
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(imageView);
    }

    /**
     * 加载图片centerCrop()
     *
     * @param path
     * @param view
     */
    public static void getImageCenterCrop(String path, ImageView view) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(view);
    }


    /**
     * Take the listener loading pictures
     *
     * @param path
     * @param imageView
     * @param requstlistener
     */
    public static void getImageWithListener(String path, ImageView imageView, RequestListener<String, GlideDrawable> requstlistener) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .listener(requstlistener)
                .placeholder(R.drawable.icon_calendar)
                .error(R.drawable.error_circle)
                .priority(Priority.NORMAL) //Download the priority
                .into(imageView);
    }

    /**
     * 清理内存缓存
     */
    public static void clearMemaryCache() {
        Glide.get(context)
                .clearMemory();
    }

    /**
     * 清理磁盘缓存
     */
    public static void clearDiskCache() {
        Glide.get(context)
                .clearDiskCache();
    }
    
}

 

 

 

 

 

 

原网站

版权声明
本文为[Xie Dong_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091203557952.html