首页
网站首页
公司简介
资讯中心
推荐内容
返回顶部
1二次封装,一分钟快速集成微信支付宝支付
发布时间:2020-03-13 22:02
浏览次数:

github:https://github.com/Shimingli/ImageLoader

www.64222.com 1image.png

采纳方法

 String url="https://upload-images.jianshu.io/upload_images/5363507-476c8bb17b124d22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"; // //圆形图片 ImageLoaderV4.getInstance().displayCircleImage(this, "http://imgsrc.baidu.com/imgad/pic/item/267f9e2f07082838b5168c32b299a9014c08f1f9.jpg", mImageView_1, R.mipmap.ic_launcher_round); //圆角图片 ImageLoaderV4.getInstance().displayRoundImage(this, "http://imgsrc.baidu.com/imgad/pic/item/267f9e2f07082838b5168c32b299a9014c08f1f9.jpg", mImageView_2, R.mipmap.ic_launcher_round, 40); //模糊图片 ImageLoaderV4.getInstance().displayBlurImage(this, "http://imgsrc.baidu.com/imgad/pic/item/267f9e2f07082838b5168c32b299a9014c08f1f9.jpg", mImageView_3, R.mipmap.ic_launcher_round, 10); //本地图片 不做处理 ImageLoaderV4.getInstance().displayImageInResource(this, R.mipmap.test, mImageView_4); // TODO: 2018/4/20 以下的三种不介意使用,需要解决一些问题 start //本地图片,模糊处理 ImageLoaderV4.getInstance().displayImageInResource(this, R.mipmap.test, mImageView_5, new BlurBitmapTranformation; //本地图片,裁圆角处理 ImageLoaderV4.getInstance().displayImageInResource(this, R.mipmap.test, mImageView_6, new GlideCircleTransformation; //四周倒角处理 ImageLoaderV4.getInstance().displayImageInResource(this, R.mipmap.test, mImageView_7, new RoundBitmapTranformation; // TODO: 2018/4/20 以上的三种不介意使用,需要解决一些问题 start //使用的是另外一种方法,指定传入的那种的方法 ,还可以不断的扩展,不断的扩展,这是Gilded提供的一些操作,牛逼 ,牛逼 这是对本地图片的操作 ImageLoaderV4.getInstance().displayImageInResourceTransform(this, R.mipmap.test, mImageView_8, new CenterInside(), R.mipmap.test); ImageLoaderV4.getInstance().displayImageInResourceTransform(this, R.mipmap.test, mImageView_9, new CircleCrop(), R.mipmap.test); ImageLoaderV4.getInstance().displayImageInResourceTransform(this, R.mipmap.test, mImageView_10, new FitCenter(), R.mipmap.test); ImageLoaderV4.getInstance().displayImageInResourceTransform(this, R.mipmap.test, mImageView_11, new RoundedCorners, R.mipmap.test); //对网络图片的操作 ImageLoaderV4.getInstance().displayImageByNet(this,url,mImageView_12, R.mipmap.test,new CenterInside; ImageLoaderV4.getInstance().displayImageByNet(this,url,mImageView_13, R.mipmap.test,new CircleCrop; ImageLoaderV4.getInstance().displayImageByNet(this,url,mImageView_14, R.mipmap.test,new FitCenter; ImageLoaderV4.getInstance().displayImageByNet(this,url,mImageView_15, R.mipmap.test,new RoundedCorners;

落实效果与利益:监听图片的下载速度,注意事项须求在监听重临键的时候,裁撤央求

 ImageLoaderV4.getInstance().disPlayImageProgress(this, "http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png", mImageView_8, R.mipmap.test, R.mipmap.test, new OnGlideImageViewListener() { @Override public void onProgress(int percent, boolean isDone, GlideException exception) { System.out.println("shiming percent="+percent); System.out.println("shiming isDone="+isDone); mProgress.setText("我在主线程,进度是多少=="+percent+"%"); if { mProgress.setText("我在主线程,进度是多少==100%"); } } }); ImageLoaderV4.getInstance().disPlayImageProgressByOnProgressListener(this, "http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png", mImageView_7, R.mipmap.test, R.mipmap.test, new OnProgressListener() { @Override public void onProgress(String imageUrl, long bytesRead, long totalBytes, boolean isDone, GlideException exception) { System.out.println("shiming bytesRead="+bytesRead); System.out.println("shiming totalBytes="+totalBytes); mProgress7.setText("我在主线程,进度是多少==+bytesRead"+bytesRead+"totalBytes="+totalBytes); if { mProgress7.setText("我在主线程,进度是多少==100%"); } } });

www.64222.com 2image.pngwww.64222.com 3image.png

动用当中提供的图片管理

www.64222.com 4image.png

投入依赖

 compile 'com.github.bumptech.glide:glide:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' compile "com.github.bumptech.glide:okhttp3-integration:4.5.0"

构建AppGlideModule

www.64222.com 5image.png

开首化registerComponents(卡塔尔(قطر‎方法,使用okhttp参预拦截器

 /** OkHttp 是一个底层网络库(相较于 Cronet 或 Volley 而言),尽管它也包含了 SPDY 的支持。 OkHttp 与 Glide 一起使用可以提供可靠的性能,并且在加载图片时通常比 Volley 产生的垃圾要少。 对于那些想要使用比 Android 提供的 HttpUrlConnection 更 nice 的 API, 或者想确保网络层代码不依赖于 app 安装的设备上 Android OS 版本的应用,OkHttp 是一个合理的选择。 如果你已经在 app 中某个地方使用了 OkHttp ,这也是选择继续为 Glide 使用 OkHttp 的一个很好的理由,就像选择其他网络库一样。 添加 OkHttp 集成库的 Gradle 依赖将使 Glide 自动开始使用 OkHttp 来加载所有来自 http 和 https URL 的图片 * @param context * @param glide * @param registry */ @Override public void registerComponents(Context context, Glide glide, Registry registry) { super.registerComponents(context, glide, registry); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getOkHttpClient; }

私下认可景况下,Glide使用 LruResourceCache , 那是 MemoryCache 接口的二个缺省完结,使用固定大小的内部存款和储蓄器和 LRU 算法。 LruResourceCache 的深浅由 Glide 的 MemorySizeCalculator 类来调控,这几个类珍视关怀设备的内存类型,设备 RAM 大小,以致显示器分辨率。应用程序可以自定义 MemoryCache 的大小, 具体是在它们的 AppGlideModule 中动用applyOptions(Context, GlideBuilder卡塔尔方法配置 MemorySizeCalculator

 //Glide会自动合理分配内存缓存,但是也可以自己手动分配。// @Override// public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {// super.applyOptions(context, builder);// //1、setMemoryCacheScreens设置MemoryCache应该能够容纳的像素值的设备屏幕数,// // 说白了就是缓存多少屏图片,默认值是2。// //todo 建议不要设置,使用glide 默认//// MemorySizeCalculator calculator = new MemorySizeCalculator.Builder//// .setMemoryCacheScreens//// .build();//// builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize;//// //2。也可以直接覆写缓存大小:todo 建议不要设置,使用glide 默认//// int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb//// builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));//// 3.甚至可以提供自己的 MemoryCache 实现://// builder.setMemoryCache(new YourAppMemoryCacheImpl;// }

Bitmap 池Glide 使用 LruBitmapPool 作为暗中同意的 BitmapPool LruBitmapPool 是一个内存中的永世大小的 BitmapPool,使用 LRU 算法清理。私下认可大小基于设备的分辨率和密度,同期也思量内部存款和储蓄器类和 isLowRamDevice 的重返值。 具体的乘除通过 Glide 的 MemorySizeCalculator 来产生,与 Glide 的 MemoryCache 的轻重检验方法日常。

// @Override// public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {// super.applyOptions(context, builder);// //1 .应用可以在它们的 AppGlideModule 中定制 [BitmapPool] 的尺寸,// // 使用 applyOptions(Context, GlideBuilder) 方法并配置 MemorySizeCalculator//// MemorySizeCalculator calculator = new MemorySizeCalculator.Builder//// .setBitmapPoolScreens//// .build();//// builder.setBitmapPool(new LruBitmapPool(calculator.getBitmapPoolSize;//// //2、应用也可以直接复写这个池的大小:////// int bitmapPoolSizeBytes = 1024 * 1024 * 30; // 30mb//// builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));////// //3、甚至可以提供 BitmapPool 的完全自定义实现:////// builder.setBitmapPool(new YourAppBitmapPoolImpl;// }

磁盘缓存Glide 使用 DiskLruCacheWrapper 作为默许的 磁盘缓存 . DiskLruCacheWrapper 是叁个应用 LRU 算法的一定大小的磁盘缓存。私下认可磁盘大小为 250 MB , 地方是在运用的 缓存文件夹 中的叁个 特定目录 。

 @Override public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) { super.applyOptions(context, builder); //假如应用程序展示的媒体内容是公开的(从无授权机制的网站上加载,或搜索引擎等), //那么应用可以将这个缓存位置改到外部存储:// builder.setDiskCache(new ExternalDiskCacheFactory; //2、无论使用内部或外部磁盘缓存,应用程序都可以改变磁盘缓存的大小:// int diskCacheSizeBytes = 1024 *1024 *100; //100 MB// builder.setDiskCache(new InternalDiskCacheFactory(context, diskCacheSizeBytes)); //3、应用程序还可以改变缓存文件夹在外存或内存上的名字:// int diskCacheSizeBytes = 1024 * 1024 *100; // 100 MB// builder.setDiskCache(new InternalDiskCacheFactory(context, cacheFolderName, diskCacheSizeBytes)); //4、应用程序还可以自行选择 DiskCache 接口的实现,并提供自己的 DiskCache.Factory 来创建缓存。 // Glide 使用一个工厂接口来在后台线程中打开 磁盘缓存 , // 这样方便缓存做诸如检查路径存在性等的IO操作而不用触发 严格模式 。// builder.setDiskCache(new DiskCache.Factory() {// @Override// public DiskCache build() {// return new YourAppCustomDiskCache();// }// }); }

/** * author: Created by shiming on 2018/4/20 15:13 * mailbox:lamshiming@sina.com 磁盘缓存 Glide 使用 DiskLruCacheWrapper 作为默认的 磁盘缓存 。 DiskLruCacheWrapper 是一个使用 LRU 算法的固定大小的磁盘缓存。默认磁盘大小为 250 MB , 位置是在应用的 缓存文件夹 中的一个 特定目录 。 假如应用程序展示的媒体内容是公开的(从无授权机制的网站上加载,或搜索引擎等), 那么应用可以将这个缓存位置改到外部存储: */class ExternalDiskCacheFactory implements DiskCache.Factory { public ExternalDiskCacheFactory(Context context) { } @Nullable @Override public DiskCache build() { return null; }}

为了保证对 Glide v3 的 GlideModules 的向后宽容性, Glide 如故会解析应用程序和持有被含有的库中的 AndroidManifest.xml 文件, 并包蕴在这里些项目清单中列出的旧 GlideModules 模块类。如若您早已搬迁到 Glide v4 的 AppGlideModule 和 LibraryGlideModule ,你能够完全禁止使用项目清单剖析。 那样可以改正 Glide 的启幕运行时间,并幸免尝试深入分析元数据时的有的暧昧难点。要禁止使用项目清单深入分析, 请在您的 AppGlideModule 完毕中复写 isManifestParsingEnabled(卡塔尔 方法:

 @Override public boolean isManifestParsingEnabled() { return false; }

IImageLoaderClient接口

/** * Created by shiming on 2016/10/26. */public interface IImageLoaderClient { public void init(Context context); public void destroy(Context context); public File getCacheDir(Context context); public void clearMemoryCache(Context context); public void clearDiskCache(Context context); public Bitmap getBitmapFromCache(Context context, String url); public void getBitmapFromCache(Context context, String url, IGetBitmapListener listener); public void displayImage(Context context, int resId, ImageView imageView); public void displayImage(Context context, String url, ImageView imageView); public void displayImage(Context context, String url, ImageView imageView, boolean isCache); public void displayImage(Fragment fragment, String url, ImageView imageView); public void displayImage(Context context, String url, ImageView imageView, int defRes); public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes); public void displayImage(Context context, String url, ImageView imageView, int defRes, BitmapTransformation transformations); public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, BitmapTransformation transformations); public void displayImage(Context context, String url, ImageView imageView, int defRes, ImageSize size); public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, ImageSize size); public void displayImage(Context context, String url, ImageView imageView, int defRes, boolean cacheInMemory); public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, boolean cacheInMemory); public void displayImage(Context context, String url, ImageView imageView, IImageLoaderListener listener); public void displayImage(Fragment fragment, String url, ImageView imageView, IImageLoaderListener listener); public void displayImage(Context context, String url, ImageView imageView, int defRes, IImageLoaderListener listener); public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, IImageLoaderListener listener); public void displayCircleImage(Context context, String url, ImageView imageView, int defRes); public void displayCircleImage(Fragment fragment, String url, ImageView imageView, int defRes); public void displayRoundImage(Context context, String url, ImageView imageView, int defRes, int radius); public void displayRoundImage(Fragment fragment, String url, ImageView imageView, int defRes, int radius); public void displayBlurImage(Context context, String url, int blurRadius, IGetDrawableListener listener); public void displayBlurImage(Context context, String url, ImageView imageView, int defRes, int blurRadius); public void displayBlurImage(Context context, int resId, ImageView imageView, int blurRadius); public void displayBlurImage(Fragment fragment, String url, ImageView imageView, int defRes, int blurRadius); public void displayImageInResource(Context context, int resId, ImageView imageView); public void displayImageInResource(Fragment fragment, int resId, ImageView imageView); public void displayImageInResource(Context context, int resId, ImageView imageView, BitmapTransformation transformations); public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, BitmapTransformation transformations); public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes); public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes); public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes, BitmapTransformation transformations); public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes, BitmapTransformation transformations); //add shiming 2018.4.20 transformation 需要装换的那种图像的风格,错误图片,或者是,正在加载中的错误图 public void displayImageInResourceTransform(Activity activity, int resId, ImageView imageView, Transformation transformation, int errorResId); public void displayImageInResourceTransform(Context context, int resId, ImageView imageView, Transformation transformation, int errorResId); public void displayImageInResourceTransform(Fragment fragment, int resId, ImageView imageView, Transformation transformation, int errorResId); //这是对网络图片,进行的图片操作,使用的glide中的方法 public void displayImageByNet(Context context, String url, ImageView imageView, int defRes,Transformation transformation); public void displayImageByNet(Fragment fragment, String url, ImageView imageView, int defRes,Transformation transformation); public void displayImageByNet(Activity activity, String url, ImageView imageView, int defRes,Transformation transformation); /** * 停止图片的加载,对某一个的Activity * @hide */ public void clear(Activity activity,ImageView imageView); /** * 停止图片的加载,context * {@hide} */ public void clear(Context context,ImageView imageView); /** * 停止图片的加载,fragment * {@hide} */ public void clear(Fragment fragment,ImageView imageView); //如果需要的话,需要指定加载中,或者是失败的图片 public void displayImageByDiskCacheStrategy(Fragment fragment, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView); public void displayImageByDiskCacheStrategy(Activity activity, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView); public void displayImageByDiskCacheStrategy(Context context, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView); //某些情形下,你可能希望只要图片不在缓存中则加载直接失败 public void disPlayImageOnlyRetrieveFromCache(Fragment fragment,String url,ImageView imageView); public void disPlayImageOnlyRetrieveFromCache(Activity activity,String url,ImageView imageView); public void disPlayImageOnlyRetrieveFromCache(Context context,String url,ImageView imageView); /** *如果你想确保一个特定的请求跳过磁盘和/或内存缓存(比如,图片验证码 –) * @param fragment * @param url * @param imageView * @param skipflag 是否跳过内存缓存 * @param diskCacheStratey 是否跳过磁盘缓存 */ public void disPlayImageSkipMemoryCache(Fragment fragment,String url,ImageView imageView,boolean skipflag,boolean diskCacheStratey); public void disPlayImageSkipMemoryCache(Activity activity,String url,ImageView imageView,boolean skipflag,boolean diskCacheStratey); public void disPlayImageSkipMemoryCache(Context context,String url,ImageView imageView,boolean skipflag,boolean diskCacheStratey); /** * 知道这个图片会加载失败,那么的话,我们可以重新加载 * @param fragment * @param url * @param fallbackUrl * @param imageView */ //从 Glide 4.3.0 开始,你可以很轻松地使用 .error() 方法。这个方法接受一个任意的 RequestBuilder,它会且只会在主请求失败时开始一个新的请求: public void disPlayImageErrorReload(Fragment fragment,String url,String fallbackUrl,ImageView imageView); public void disPlayImageErrorReload(Activity activity,String url,String fallbackUrl,ImageView imageView); public void disPlayImageErrorReload(Context context,String url,String fallbackUrl,ImageView imageView); /** 未来 Glide 将默认加载硬件位图而不需要额外的启用配置,只保留禁用的选项 现在已经默认开启了这个配置,但是在有些情况下需要关闭 所以提供了以下的方法,禁用硬件位图 disallowHardwareConfig * @param fragment * @param url * @param imageView */// 哪些情况不能使用硬件位图?// 在显存中存储像素数据意味着这些数据不容易访问到,在某些情况下可能会发生异常。已知的情形列举如下:// 在 Java 中读写像素数据,包括:// Bitmap#getPixel// Bitmap#getPixels// Bitmap#copyPixelsToBuffer// Bitmap#copyPixelsFromBuffer// 在本地  代码中读写像素数据// 使用软件画布 (software Canvas) 渲染硬件位图:// Canvas canvas = new Canvas(normalBitmap)//canvas.drawBitmap(hardwareBitmap, 0, 0, new Paint;// 在绘制位图的 View 上使用软件层 (software layer type) // ImageView imageView = …// imageView.setImageBitmap(hardwareBitmap);//imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);// 打开过多的文件描述符 . 每个硬件位图会消耗一个文件描述符。// 这里存在一个每个进程的文件描述符限制 ( Android O 及更早版本一般为 1024,在某些 O-MR1 和更高的构建上是 32K)。// Glide 将尝试限制分配的硬件位图以保持在这个限制以内,但如果你已经分配了大量的文件描述符,这可能是一个问题。// 需要ARGB_8888 Bitmaps 作为前置条件// 在代码中触发截屏操作,它会尝试使用 Canvas 来绘制视图层级。// 作为一个替代方案,在 Android O 以上版本你可以使用 PixelCopy.// 共享元素过渡 (shared element transition) public void disPlayImagedisallowHardwareConfig(Fragment fragment,String url,ImageView imageView); public void disPlayImagedisallowHardwareConfig(Activity activity,String url,ImageView imageView); public void disPlayImagedisallowHardwareConfig(Context context,String url,ImageView imageView); //监听图片的下载进度,是否完成,百分比 也可以加载本地图片,扩张一下 public void disPlayImageProgress(Context context,String url,ImageView imageView,int placeholderResId,int errorResId,OnGlideImageViewListener listener); public void disPlayImageProgress(Activity activity,String url,ImageView imageView,int placeholderResId,int errorResId,OnGlideImageViewListener listener); public void disPlayImageProgress(Fragment fragment,String url,ImageView imageView,int placeholderResId,int errorResId,OnGlideImageViewListener listener); public void disPlayImageProgressByOnProgressListener(Context context,String url,ImageView imageView,int placeholderResId,int errorResId, OnProgressListener onProgressListener); public void disPlayImageProgressByOnProgressListener(Activity activity,String url,ImageView imageView,int placeholderResId,int errorResId, OnProgressListener onProgressListener); public void disPlayImageProgressByOnProgressListener(Fragment fragment,String url,ImageView imageView,int placeholderResId,int errorResId, OnProgressListener onProgressListener);// TransitionOptions 用于给一个特定的请求指定过渡。// 每个请求可以使用 RequestBuilder 中的 transition()// 方法来设定 TransitionOptions 。还可以通过使用// BitmapTransitionOptions 或 DrawableTransitionOptions// 来指定类型特定的过渡动画。对于 Bitmap 和 Drawable// 之外的资源类型,可以使用 GenericTransitionOptions。 Glide v4 将不会默认应用交叉淡入或任何其他的过渡效果。每个请求必须手动应用过渡。 public void displayImageByTransition(Context context, String url, TransitionOptions transitionOptions, ImageView imageView); public void displayImageByTransition(Activity activity, String url, TransitionOptions transitionOptions, ImageView imageView); public void displayImageByTransition(Fragment fragment, String url, TransitionOptions transitionOptions, ImageView imageView); //失去焦点,建议实际的项目中少用,取消求情 public void glidePauseRequests(Context context); public void glidePauseRequests(Activity activity); public void glidePauseRequests(Fragment fragment); //获取焦点,建议实际的项目中少用 public void glideResumeRequests(Context context); public void glideResumeRequests(Activity activity); public void glideResumeRequests(Fragment fragment); //加载缩图图 int thumbnailSize = 10;//越小,图片越小,低网络的情况,图片越小 //GlideApp.with.load(urlnoData).override(thumbnailSize))// API 来强制 Glide 在缩略图请求中加载一个低分辨率图像 public void displayImageThumbnail(Context context,String url,String backUrl,int thumbnailSize,ImageView imageView); public void displayImageThumbnail(Activity activity,String url,String backUrl,int thumbnailSize,ImageView imageView); public void displayImageThumbnail(Fragment fragment,String url,String backUrl,int thumbnailSize,ImageView imageView); //如果没有两个url的话,也想,记载一个缩略图 public void displayImageThumbnail(Fragment fragment,String url,float thumbnailSize,ImageView imageView); public void displayImageThumbnail(Activity activity,String url,float thumbnailSize,ImageView imageView); public void displayImageThumbnail(Context context,String url,float thumbnailSize,ImageView imageView);}

GlideImageLoaderClient实现类

package code.shiming.com.imageloader471;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.os.Handler;import android.os.Looper;import android.os.SystemClock;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.annotation.UiThread;import android.support.v4.app.Fragment;import android.util.Log;import android.widget.ImageView;import com.bumptech.glide.Glide;import com.bumptech.glide.TransitionOptions;import com.bumptech.glide.load.DataSource;import com.bumptech.glide.load.Transformation;import com.bumptech.glide.load.engine.DiskCacheStrategy;import com.bumptech.glide.load.engine.GlideException;import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;import com.bumptech.glide.request.RequestListener;import com.bumptech.glide.request.RequestOptions;import com.bumptech.glide.request.target.SimpleTarget;import com.bumptech.glide.request.target.Target;import com.bumptech.glide.request.transition.Transition;import java.io.File;import code.shiming.com.imageloader471.listener.IGetBitmapListener;import code.shiming.com.imageloader471.listener.IGetDrawableListener;import code.shiming.com.imageloader471.listener.IImageLoaderListener;import code.shiming.com.imageloader471.listener.ImageSize;import code.shiming.com.imageloader471.okhttp.OnGlideImageViewListener;import code.shiming.com.imageloader471.okhttp.OnProgressListener;import code.shiming.com.imageloader471.okhttp.ProgressManager;import code.shiming.com.imageloader471.tranform.BlurBitmapTranformation;import code.shiming.com.imageloader471.tranform.GlideCircleTransformation;import code.shiming.com.imageloader471.tranform.RoundBitmapTranformation;/*** Created by shiming on 2016/10/26.* des:* with(Context context). 使用Application上下文,Glide请求将不受Activity/Fragment生命周期控制。 with(Activity activity).使用Activity作为上下文,Glide的请求会受到Activity生命周期控制。 with(FragmentActivity activity).Glide的请求会受到FragmentActivity生命周期控制。 with(android.app.Fragment fragment).Glide的请求会受到Fragment 生命周期控制。 with(android.support.v4.app.Fragment fragment).Glide的请求会受到Fragment生命周期控制。*/public class GlideImageLoaderClient implements IImageLoaderClient { private static final String TAG ="GlideImageLoaderClient"; @Override public void init(Context context) { } @Override public void destroy(Context context) { clearMemoryCache; } @Override public File getCacheDir(Context context) { return Glide.getPhotoCacheDir; } /** * 使用ui线程 * @param context */ @UiThread @Override public void clearMemoryCache(Context context) { GlideApp.get.clearMemory(); } @SuppressLint("StaticFieldLeak") @Override public void clearDiskCache(final Context context) { new AsyncTask<Void, Void, Void> (){ @Override protected Void doInBackground(Void... params) { //必须在子线程中 This method must be called on a background thread. Glide.get.clearDiskCache(); return null; } }; } @Override public Bitmap getBitmapFromCache(Context context, String url) { throw new UnsupportedOperationException("glide 不支持同步 获取缓存中 bitmap"); } /** * 获取缓存中的图片 * @param context * @param url * @param listener */ @Override public void getBitmapFromCache(Context context, String url, final IGetBitmapListener listener) { GlideApp.with.asBitmap().load.into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { if (listener != null) { listener.onBitmap; } } }); } /** * 默认的策略是DiskCacheStrategy.AUTOMATIC DiskCacheStrategy.ALL 使用DATA和RESOURCE缓存远程数据,仅使用RESOURCE来缓存本地数据。 DiskCacheStrategy.NONE 不使用磁盘缓存 DiskCacheStrategy.DATA 在资源解码前就将原始数据写入磁盘缓存 DiskCacheStrategy.RESOURCE 在资源解码后将数据写入磁盘缓存,即经过缩放等转换后的图片资源。 DiskCacheStrategy.AUTOMATIC 根据原始图片数据和资源编码策略来自动选择磁盘缓存策略。 * @param context 上下文 * @param resId id * @param imageView into */ //DiskCacheStrategy.SOURCE:缓存原始数据 DiskCacheStrategy.DATA对应Glide 3中的DiskCacheStrategy.SOURCE @Override public void displayImage(Context context, int resId, ImageView imageView) { //设置缓存策略缓存原始数据 Saves just the original data to cache GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(imageView); } /** * * @param context * @param url url * @param imageView in */ @Override public void displayImage(Context context, String url, ImageView imageView) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(imageView); } /** * * @param context * @param url * @param imageView * @param isCache 是否是缓存 如果是:缓存策略缓存原始数据 不是的话 :缓存策略DiskCacheStrategy.NONE:什么都不缓存 */ @Override public void displayImage(Context context, String url, ImageView imageView, boolean isCache) { GlideApp.with.load.skipMemoryCache.diskCacheStrategy(isCache ? DiskCacheStrategy.AUTOMATIC : DiskCacheStrategy.NONE).into(imageView); } /** * * @param fragment 绑定生命周期 * @param url * @param imageView */ @Override public void displayImage(Fragment fragment, String url, ImageView imageView) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(imageView); } /** * 使用.placeholder()方法在某些情况下会导致图片显示的时候出现图片变形的情况 * 这是因为Glide默认开启的crossFade动画导致的TransitionDrawable绘制异常 * @param context * @param url * @param imageView * @param defRes defRes 可以是个new ColorDrawable(Color.BLACK) 也可以是张图片 */ //默认为200 时间有点长 ,工程中要修改下,设置一个加载失败和加载中的动画过渡,V4.0的使用的方法 @Override public void displayImage(Context context, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).transition(new DrawableTransitionOptions().crossFade.placeholder.error.into(imageView); } /** * 默认时间为200 需 * @param fragment * @param url * @param imageView * @param defRes */ @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).transition(new DrawableTransitionOptions().crossFade.placeholder.error.into(imageView); } /** * * @param context * @param url * @param imageView * @param defRes * @param transformations bitmapTransform 方法设置图片转换 */ @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, final BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(requestOptionsTransformation(defRes,defRes,transformations)).into(imageView); } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(requestOptionsTransformation(defRes,defRes,transformations)).into(imageView); } /** * 加载原圆形图片 * @param context * @param url * @param imageView * @param defRes */ public void displayImageCircle(Context context, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(circleRequestOptions(defRes,defRes)).into(imageView); } public void displayImageCircle(Fragment context, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.DATA).apply(circleRequestOptions(defRes,defRes)).into(imageView); } public RequestOptions requestOptions(int placeholderResId, int errorResId) { return new RequestOptions() .placeholder(placeholderResId) .error(errorResId); } public RequestOptions requestOptionsTransformation(int placeholderResId, int errorResId,BitmapTransformation bitmapTransformation) { return requestOptions(placeholderResId,errorResId) .transform(bitmapTransformation); } /** * 加载原图 * @param placeholderResId * @param errorResId * @return */ public RequestOptions circleRequestOptions(int placeholderResId, int errorResId) { return requestOptions(placeholderResId, errorResId) .transform(new GlideCircleTransformation; } public RequestOptions roundRequestOptions(int placeholderResId, int errorResId,int radius) { return requestOptions(placeholderResId, errorResId) .transform(new RoundBitmapTranformation; } /** * * @param context * @param url * @param imageView * @param defRes placeholder(int resourceId). 设置资源加载过程中的占位Drawable error(int resourceId).设置load失败时显示的Drawable * @param size override(int width, int height). 重新设置Target的宽高值 */ @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, ImageSize size) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).placeholder.error.override(size.getWidth(), size.getHeight.into(imageView); } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, ImageSize size) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).placeholder.error.override(size.getWidth(), size.getHeight.into(imageView); } /** * .skipMemoryCache去特意告诉Glide跳过内存缓存 是否跳过内存,还是不跳过 * @param context * @param url * @param imageView * @param defRes * @param cacheInMemory */ @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, boolean cacheInMemory) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).placeholder.error.skipMemoryCache(cacheInMemory).into(imageView); } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, boolean cacheInMemory) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).placeholder.error.skipMemoryCache(cacheInMemory).into(imageView); } /** * 只在需要的地方进行监听 listener 通过自定义的接口回调参数 * @param context * @param url * @param imageView * @param listener 监听资源加载的请求状态 但不要每次请求都使用新的监听器,要避免不必要的内存申请, * 可以使用单例进行统一的异常监听和处理 */ @Override public void displayImage(Context context, final String url, final ImageView imageView, final IImageLoaderListener listener) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { listener.onLoadingFailed(url, imageView, e); Log.e(TAG, "Load failed", e);//如果关系的话,关系如何失败 return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { listener.onLoadingComplete(url, imageView); return false; } }).into(imageView); } @Override public void displayImage(Fragment fragment, final String url, final ImageView imageView, final IImageLoaderListener listener) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { listener.onLoadingFailed(url, imageView, e); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { listener.onLoadingComplete(url, imageView); return false; } }).into(imageView); } @Override public void displayImage(Context context, final String url, final ImageView imageView, int defRes, final IImageLoaderListener listener) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).placeholder.error.listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { listener.onLoadingFailed(url, imageView, e); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { listener.onLoadingComplete(url, imageView); return false; } }).into(imageView); } @Override public void displayImage(Fragment fragment, final String url, final ImageView imageView, int defRes, final IImageLoaderListener listener) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { listener.onLoadingFailed(url, imageView, e); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { listener.onLoadingComplete(url, imageView); return false; } }).into(imageView); } /** * 圆形图片的裁剪 * @param context * @param url * @param imageView * @param defRes */ @Override public void displayCircleImage(Context context, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(circleRequestOptions(defRes,defRes)).into(imageView); } @Override public void displayCircleImage(Fragment fragment, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(circleRequestOptions(defRes,defRes)).into(imageView); } public void displayCircleImage(Activity fragment, String url, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.DATA).apply(circleRequestOptions(defRes,defRes)).into(imageView); } /** * * @param context * @param url * @param imageView * @param defRes * @param radius 倒圆角的图片 需要传入需要radius 越大的话,倒角越明显 */ @Override public void displayRoundImage(Context context, String url, ImageView imageView, int defRes, int radius) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(roundRequestOptions(defRes,defRes,radius)).into(imageView); } @Override public void displayRoundImage(Fragment fragment, String url, ImageView imageView, int defRes, int radius) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.DATA).apply(roundRequestOptions(defRes,defRes,radius)).into(imageView); } /** * * @param context * @param url * @param blurRadius 模糊的程度 ,数字越大越模糊 * @param listener 接口回调需要拿到drawable */ @Override public void displayBlurImage(Context context, String url, int blurRadius, final IGetDrawableListener listener) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { if ( listener != null ) { listener.onDrawable( resource ); } } }); } /** * 不需要关系此模糊图的drawable * @param context * @param url * @param imageView * @param defRes * @param blurRadius */ @Override public void displayBlurImage(Context context, String url, ImageView imageView, int defRes, int blurRadius) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(blurRequestOptions(defRes,defRes,blurRadius)).into(imageView); } private RequestOptions blurRequestOptions(int defRes, int defRes1, int blurRadius) { return requestOptions(defRes, defRes1) .transform(new BlurBitmapTranformation(blurRadius)); } @Override public void displayBlurImage(Context context, int resId, ImageView imageView, int blurRadius) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(blurRequestOptions(resId,resId,blurRadius)).into(imageView); } @Override public void displayBlurImage(Fragment fragment, String url, ImageView imageView, int defRes, int blurRadius) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(blurRequestOptions(defRes,defRes,blurRadius)).into(imageView); } /** * 加载资源文件 * @param context * @param resId * @param imageView */ @Override public void displayImageInResource(Context context, int resId, ImageView imageView) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView); } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView); } public void displayImageInResource(Activity fragment, int resId, ImageView imageView) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView); } /** * * @param fragment * @param resId * @param imageView * @param transformation 需要变换那种图像 * @param errorResId */ @Override public void displayImageInResourceTransform(Activity fragment, int resId, ImageView imageView,Transformation transformation,int errorResId) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).apply(requestOptionsTransform(errorResId,errorResId,transformation)).into(imageView); } @Override public void displayImageInResourceTransform(Context context, int resId, ImageView imageView, Transformation transformation, int errorResId) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).apply(requestOptionsTransform(errorResId,errorResId,transformation)).into(imageView); } @Override public void displayImageInResourceTransform(Fragment fragment, int resId, ImageView imageView, Transformation transformation, int errorResId) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).apply(requestOptionsTransform(errorResId,errorResId,transformation)).into(imageView); } @Override public void displayImageByNet(Context context, String url, ImageView imageView, int defRes, Transformation transformation) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(requestOptionsTransform(defRes,defRes,transformation)).into(imageView); } @Override public void displayImageByNet(Fragment fragment, String url, ImageView imageView, int defRes, Transformation transformation) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(requestOptionsTransform(defRes,defRes,transformation)).into(imageView); } @Override public void displayImageByNet(Activity activity, String url, ImageView imageView, int defRes, Transformation transformation) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).apply(requestOptionsTransform(defRes,defRes,transformation)).into(imageView); } @Override public void clear(Activity activity, ImageView imageView) { GlideApp.with.clear(imageView); } @Override public void clear(Context context, ImageView imageView) { GlideApp.with.clear(imageView); } @Override public void clear(Fragment fragment, ImageView imageView) { GlideApp.with.clear(imageView); }// 默认的策略是DiskCacheStrategy.AUTOMATIC// DiskCacheStrategy.ALL 使用DATA和RESOURCE缓存远程数据,仅使用RESOURCE来缓存本地数据。// DiskCacheStrategy.NONE 不使用磁盘缓存// DiskCacheStrategy.DATA 在资源解码前就将原始数据写入磁盘缓存// DiskCacheStrategy.RESOURCE 在资源解码后将数据写入磁盘缓存,即经过缩放等转换后的图片资源。// DiskCacheStrategy.AUTOMATIC 根据原始图片数据和资源编码策略来自动选择磁盘缓存策略。 @Override public void displayImageByDiskCacheStrategy(Fragment fragment, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView) { GlideApp.with.load.diskCacheStrategy(diskCacheStrategy).into(imageView); }// DiskCacheStrategy.NONE: 表示不缓存任何内容。// DiskCacheStrategy.DATA: 表示只缓存原始图片。// DiskCacheStrategy.RESOURCE: 表示只缓存转换过后的图片。// DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。// DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略。 @Override public void displayImageByDiskCacheStrategy(Activity activity, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView) { GlideApp.with.load.diskCacheStrategy(diskCacheStrategy).into(imageView); } @Override public void displayImageByDiskCacheStrategy(Context context, String url, DiskCacheStrategy diskCacheStrategy,ImageView imageView) { GlideApp.with.load.diskCacheStrategy(diskCacheStrategy).into(imageView); } @Override public void disPlayImageOnlyRetrieveFromCache(Fragment fragment, String url, ImageView imageView) { GlideApp.with .load .onlyRetrieveFromCache .into(imageView); } @Override public void disPlayImageOnlyRetrieveFromCache(Activity activity, String url, ImageView imageView) { GlideApp.with .load .onlyRetrieveFromCache .into(imageView); } @Override public void disPlayImageOnlyRetrieveFromCache(Context context, String url, ImageView imageView) { GlideApp.with .load .onlyRetrieveFromCache .into(imageView); } /** *如果你想确保一个特定的请求跳过磁盘和/或内存缓存(比如,图片验证码 –) * @param fragment * @param url * @param imageView * @param skipflag 是否跳过内存缓存 * @param diskCacheStratey 是否跳过磁盘缓存 */ @Override public void disPlayImageSkipMemoryCache(Fragment fragment, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { GlideApp.with .load .diskCacheStrategy(diskCacheStratey?DiskCacheStrategy.NONE:DiskCacheStrategy.AUTOMATIC) .skipMemoryCache .into(imageView); } @Override public void disPlayImageSkipMemoryCache(Activity activity, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { GlideApp.with .load .diskCacheStrategy(diskCacheStratey?DiskCacheStrategy.NONE:DiskCacheStrategy.AUTOMATIC) .skipMemoryCache .into(imageView); } @Override public void disPlayImageSkipMemoryCache(Context context, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { GlideApp.with .load .diskCacheStrategy(diskCacheStratey?DiskCacheStrategy.NONE:DiskCacheStrategy.AUTOMATIC) .skipMemoryCache .into(imageView); } @Override public void disPlayImageErrorReload(Fragment fragment, String url, String fallbackUrl, ImageView imageView) { GlideApp.with .load .error(GlideApp.with .load(fallbackUrl)) .into(imageView); } @Override public void disPlayImageErrorReload(Activity activity, String url, String fallbackUrl, ImageView imageView) { GlideApp.with .load .error(GlideApp.with .load(fallbackUrl)) .into(imageView); } @Override public void disPlayImageErrorReload(Context context, String url, String fallbackUrl, ImageView imageView) { GlideApp.with .load .error(GlideApp.with .load(fallbackUrl)) .into(imageView); } @Override public void disPlayImagedisallowHardwareConfig(Fragment fragment, String url, ImageView imageView) { GlideApp.with .load .disallowHardwareConfig() .into(imageView); //第二种方法// RequestOptions options = new RequestOptions().disallowHardwareConfig();// Glide.with// .load// .apply// .into(imageView); } @Override public void disPlayImagedisallowHardwareConfig(Activity activity, String url, ImageView imageView) { GlideApp.with .load .disallowHardwareConfig() .into(imageView); } @Override public void disPlayImagedisallowHardwareConfig(Context context, String url, ImageView imageView) { GlideApp.with .load .disallowHardwareConfig() .into(imageView); } //监听进度 @Override public void disPlayImageProgress(Context context, final String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 onGlideImageViewListener=listener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void disPlayImageProgress(Activity activity, final String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 onGlideImageViewListener=listener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void disPlayImageProgress(Fragment fragment, final String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 onGlideImageViewListener=listener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void disPlayImageProgressByOnProgressListener(Context context, final String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 this.onProgressListener = onProgressListener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void disPlayImageProgressByOnProgressListener(Activity activity, final String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 this.onProgressListener = onProgressListener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void disPlayImageProgressByOnProgressListener(Fragment fragment, final String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//todo 我是为了测试,看到进度条,才把缓存策略设置成这样的,项目中一定不要这样做 .apply(new RequestOptions() .placeholder(placeholderResId) .error(errorResId)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, e); ProgressManager.removeProgressListener(internalProgressListener); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { mainThreadCallback(url,mLastBytesRead, mTotalBytes, true, null); ProgressManager.removeProgressListener(internalProgressListener); return false; } }).into(imageView); //赋值 上去 this.onProgressListener = onProgressListener; mMainThreadHandler = new Handler(Looper.getMainLooper; internalProgressListener = new OnProgressListener() { @Override public void onProgress(String imageUrl, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { if (totalBytes == 0) return; if (mLastBytesRead == bytesRead && mLastStatus == isDone) return; mLastBytesRead = bytesRead; mTotalBytes = totalBytes; mLastStatus = isDone; mainThreadCallback(imageUrl,bytesRead, totalBytes, isDone, exception); if  { ProgressManager.removeProgressListener; } } }; ProgressManager.addProgressListener(internalProgressListener); } @Override public void displayImageByTransition(Context context, String url, TransitionOptions transitionOptions, ImageView imageView) { if (transitionOptions instanceof DrawableTransitionOptions){ GlideApp.with .load .transition((DrawableTransitionOptions)transitionOptions) .into(imageView); }else { GlideApp.with .asBitmap() .load .transition(transitionOptions) .into(imageView); } } @Override public void displayImageByTransition(Activity activity, String url, TransitionOptions transitionOptions, ImageView imageView) { if (transitionOptions instanceof DrawableTransitionOptions){ GlideApp.with .load .transition((DrawableTransitionOptions)transitionOptions) .into(imageView); }else { GlideApp.with .asBitmap() .load .transition(transitionOptions) .into(imageView); } } @Override public void displayImageByTransition(Fragment fragment, String url, TransitionOptions transitionOptions, ImageView imageView) { if (transitionOptions instanceof DrawableTransitionOptions){ GlideApp.with .load .transition((DrawableTransitionOptions)transitionOptions) .into(imageView); }else { GlideApp.with .asBitmap() .load .transition(transitionOptions) .into(imageView); } } @Override public void glidePauseRequests(Context context) { GlideApp.with.pauseRequests(); } @Override public void glidePauseRequests(Activity activity) { GlideApp.with.pauseRequests(); } @Override public void glidePauseRequests(Fragment fragment) { GlideApp.with.pauseRequests(); } @Override public void glideResumeRequests(Context context) { GlideApp.with.resumeRequests(); } @Override public void glideResumeRequests(Activity activity) { GlideApp.with.resumeRequests(); } @Override public void glideResumeRequests(Fragment fragment) { GlideApp.with.resumeRequests(); } /** * * 加载缩略图 * @param context * @param url 图片url * @param backUrl 缩略图的url * @param thumbnailSize 如果需要放大放小的数值 * @param imageView */ @Override public void displayImageThumbnail(Context context, String url, String backUrl, int thumbnailSize, ImageView imageView) { if(thumbnailSize == 0) { GlideApp.with .load .thumbnail(Glide.with .load .into(imageView); }else { //越小,图片越小,低网络的情况,图片越小 GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//为了测试不缓存 .thumbnail(GlideApp.with .load .override(thumbnailSize))// API 来强制 Glide 在缩略图请求中加载一个低分辨率图像 .into(imageView); } } @Override public void displayImageThumbnail(Activity activity, String url, String backUrl, int thumbnailSize, ImageView imageView) { if(thumbnailSize == 0) { GlideApp.with .load .thumbnail(Glide.with .load .into(imageView); }else { //越小,图片越小,低网络的情况,图片越小 GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//为了测试不缓存 .thumbnail(GlideApp.with .load .override(thumbnailSize))// API 来强制 Glide 在缩略图请求中加载一个低分辨率图像 .into(imageView); } } @Override public void displayImageThumbnail(Fragment fragment, String url, String backUrl, int thumbnailSize, ImageView imageView) { if(thumbnailSize == 0) { GlideApp.with .load .thumbnail(Glide.with .load .into(imageView); }else { //越小,图片越小,低网络的情况,图片越小 GlideApp.with .load .diskCacheStrategy(DiskCacheStrategy.NONE)//为了测试不缓存 .thumbnail(GlideApp.with .load .override(thumbnailSize))// API 来强制 Glide 在缩略图请求中加载一个低分辨率图像 .into(imageView); } } /** * * thumbnail 方法有一个简化版本,它只需要一个 sizeMultiplier 参数。 * 如果你只是想为你的加载相同的图片,但尺寸为 View 或 Target 的某个百分比的话特别有用: * @param fragment * @param url * @param thumbnailSize * @param imageView */ @Override public void displayImageThumbnail(Fragment fragment, String url, float thumbnailSize, ImageView imageView) { if(thumbnailSize >= 0.0F && thumbnailSize <= 1.0F) { GlideApp.with .load .thumbnail(/*sizeMultiplier=*/ thumbnailSize) .into(imageView); } else { throw new IllegalArgumentException("thumbnailSize 的值必须在0到1之间"); } } @Override public void displayImageThumbnail(Activity activity, String url, float thumbnailSize, ImageView imageView) { if(thumbnailSize >= 0.0F && thumbnailSize <= 1.0F) { GlideApp.with .load .thumbnail(/*sizeMultiplier=*/ thumbnailSize) .into(imageView); } else { throw new IllegalArgumentException("thumbnailSize 的值必须在0到1之间"); } } @Override public void displayImageThumbnail(Context context, String url, float thumbnailSize, ImageView imageView) { if(thumbnailSize >= 0.0F && thumbnailSize <= 1.0F) { GlideApp.with .load .thumbnail(/*sizeMultiplier=*/ thumbnailSize) .into(imageView); } else { throw new IllegalArgumentException("thumbnailSize 的值必须在0到1之间"); } } private Handler mMainThreadHandler; private long mTotalBytes = 0; private long mLastBytesRead = 0; private void mainThreadCallback(final String url, final long bytesRead, final long totalBytes, final boolean isDone, final GlideException exception) { mMainThreadHandler.post(new Runnable() { @Override public void run() { // TODO: 2018/4/20 放慢效果 实际的项目中给我去掉 SystemClock.sleep; final int percent =  ((bytesRead * 1.0f / totalBytes) * 100.0f); if (onProgressListener != null) { onProgressListener.onProgress(url, bytesRead, totalBytes, isDone, exception); } if (onGlideImageViewListener != null) { onGlideImageViewListener.onProgress(percent, isDone, exception); } } }); } private boolean mLastStatus = false; private OnProgressListener internalProgressListener; private OnGlideImageViewListener onGlideImageViewListener; private OnProgressListener onProgressListener; /** * 指定传入的那种图片的变形 * @param placeholderResId * @param errorResId * @param transformation * @return */ public RequestOptions requestOptionsTransform(int placeholderResId, int errorResId,Transformation transformation) { return new RequestOptions() .placeholder(placeholderResId) .error(errorResId).transform(transformation); } /** * 加载资源文件的同时,对图片进行处理 * @param context * @param resId * @param imageView * @param transformations */ @Override public void displayImageInResource(Context context, int resId, ImageView imageView, BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).transform(transformations).into(imageView); } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).transform(transformations).into(imageView); } /** * 当传入到其他的东西的时候,我要保证图片不变形 * @param placeholderResId * @param errorResId * @param transformation * @return */ public RequestOptions requestOptionsNoTransform(int placeholderResId, int errorResId, Transformation<Bitmap> transformation) { return new RequestOptions() .placeholder(placeholderResId) .error(errorResId).transform(transformation); } /** * 加载资源文件失败了,加载中的默认图和失败的图片 * @param context * @param resId * @param imageView * @param defRes */ @Override public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).placeholder.error.into(imageView); } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).placeholder.error.into(imageView); } //关心context @Override public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes, BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).placeholder.error.transform(transformations).into(imageView); } //关心fragment @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes, BitmapTransformation transformations) { GlideApp.with.load.diskCacheStrategy(DiskCacheStrategy.NONE).placeholder.error.transform(transformations).into(imageView); }}

ImageLoaderV4调用类

package code.shiming.com.imageloader471;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.support.v4.app.Fragment;import android.widget.ImageView;import com.bumptech.glide.TransitionOptions;import com.bumptech.glide.load.Transformation;import com.bumptech.glide.load.engine.DiskCacheStrategy;import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;import java.io.File;import code.shiming.com.imageloader471.listener.IGetBitmapListener;import code.shiming.com.imageloader471.listener.IGetDrawableListener;import code.shiming.com.imageloader471.listener.IImageLoaderListener;import code.shiming.com.imageloader471.listener.ImageSize;import code.shiming.com.imageloader471.okhttp.OnGlideImageViewListener;import code.shiming.com.imageloader471.okhttp.OnProgressListener;/** * Created by shiming on 2016/10/26. */public class ImageLoaderV4 implements IImageLoaderClient { /** * volatile 关键字:我个人理解的是:使用volatile关键字的程序在并发时能够正确执行。 * 但是它不能够代替synchronized关键字。在网上找到这么一句话: * 观察加入volatile关键字和没有加入volatile关键字时所生成的汇编代码发现, * 加入volatile关键字时,会多出一个lock前缀指令lock前缀指令实际上相当于一个内存屏障, * 内存屏障会提供3个功能:1)它确保指令重排序时不会把其后面的指令排到内存屏障之前的位置, * 也不会把前面的指令排到内存屏障的后面;即在执行到内存屏障这句指令时,在它前面的操作已经全部完成; * 2)它会强制将对缓存的修改操作立即写入主存;3)如果是写操作,它会导致其他CPU中对应的缓存行无效。 */ private volatile static ImageLoaderV4 instance; private IImageLoaderClient client; private ImageLoaderV4() { client = new GlideImageLoaderClient(); } /*** * 设置 图片加载库客户端 */ public void setImageLoaderClient(Context context, IImageLoaderClient client) { if (this.client != null) { this.client.clearMemoryCache; } if (this.client != client) { this.client = client; if (this.client != null) { this.client.init; } } } /** * 因为每次调用实例都需要判断同步锁,很多项目包括很多人都是用这种的 * 双重判断校验的方法,这种的方法看似很完美的解决了效率的问题,但是它 * 在并发量不多,安全性不太高的情况下能完美的运行,但是, * 在jvm编译的过程中会出现指令重排的优化过程,这就会导致singleton实际上 * 没有被初始化,就分配了内存空间,也就是说singleton!=null但是又没有被初始化, * 这就会导致返回的singletonthird返回的是不完整的 * 参考:http://www.360doc.com/content/11/0810/12/1542811_139352888.shtml * 建议使用内部类实现单利模式 */ public static ImageLoaderV4 getInstance() { if (instance == null) { synchronized (ImageLoaderV4.class) { if (instance == null) { instance = new ImageLoaderV4(); } } } return instance; } @Override public void init(Context context) { } @Override public void destroy(Context context) { if (client != null) { client.destroy; client = null; } instance = null; } @Override public File getCacheDir(Context context) { if (client != null) { return client.getCacheDir; } return null; } @Override public void clearMemoryCache(Context context) { if (client != null) { client.clearMemoryCache; } } @Override public void clearDiskCache(Context context) { if (client != null) { client.clearDiskCache; } } @Override public Bitmap getBitmapFromCache(Context context, String url) { if (client != null) { return client.getBitmapFromCache(context, url); } return null; } /** * 不是 * @param context * @param url * @param listener */ @Override public void getBitmapFromCache(Context context, String url, IGetBitmapListener listener) { if (client != null) { client.getBitmapFromCache(context, url, listener); } } @Override public void displayImage(Context context, int resId, ImageView imageView) { if (client != null) { client.displayImage(context, resId, imageView); } } @Override public void displayImage(Context context, String url, ImageView imageView) { if (client != null) { client.displayImage(context, url, imageView); } } @Override public void displayImage(Context context, String url, ImageView imageView, boolean isCache) { if (client != null) { client.displayImage(context, url, imageView,isCache); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView) { if (client != null) { client.displayImage(fragment, url, imageView); } } @Override public void displayImage(Context context, String url, ImageView imageView, int defRes) { if (client != null) { client.displayImage(context, url, imageView, defRes); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes) { if (client != null) { client.displayImage(fragment, url, imageView, defRes); } } @Override public void displayImage(Context context, String url, ImageView imageView, int defRes,BitmapTransformation transformations) { if (client != null) { client.displayImage(context, url, imageView, defRes, transformations); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, BitmapTransformation transformations) { if (client != null) { client.displayImage(fragment, url, imageView, defRes, transformations); } } @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, ImageSize size) { if (client != null) { client.displayImage(context, url, imageView, defRes, size); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, ImageSize size) { if (client != null) { client.displayImage(fragment, url, imageView, defRes, size); } } @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, boolean cacheInMemory) { if (client != null) { client.displayImage(context, url, imageView, defRes, cacheInMemory); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, boolean cacheInMemory) { if (client != null) { client.displayImage(fragment, url, imageView, defRes, cacheInMemory); } } @Override public void displayImage(Context context, String url, ImageView imageView, IImageLoaderListener listener) { if (client != null) { client.displayImage(context, url, imageView, listener); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, IImageLoaderListener listener) { if (client != null) { client.displayImage(fragment, url, imageView, listener); } } @Override public void displayImage(Context context, String url, ImageView imageView, int defRes, IImageLoaderListener listener) { if (client != null) { client.displayImage(context, url, imageView, defRes, listener); } } @Override public void displayImage(Fragment fragment, String url, ImageView imageView, int defRes, IImageLoaderListener listener) { if (client != null) { client.displayImage(fragment, url, imageView, defRes, listener); } } @Override public void displayCircleImage(Context context, String url, ImageView imageView, int defRes) { if (client != null) { client.displayCircleImage(context, url, imageView, defRes); } } @Override public void displayCircleImage(Fragment fragment, String url, ImageView imageView, int defRes) { if (client != null) { client.displayCircleImage(fragment, url, imageView, defRes); } } @Override public void displayRoundImage(Context context, String url, ImageView imageView, int defRes, int radius) { if (client != null) { client.displayRoundImage(context, url, imageView, defRes, radius); } } @Override public void displayBlurImage(Context context, String url, int blurRadius, final IGetDrawableListener listener) { if (client != null) { client.displayBlurImage(context, url, blurRadius, listener); } } @Override public void displayRoundImage(Fragment fragment, String url, ImageView imageView, int defRes, int radius) { if (client != null) { client.displayRoundImage(fragment, url, imageView, defRes, radius); } } @Override public void displayBlurImage(Context context, String url, ImageView imageView, int defRes, int blurRadius) { if (client != null) { client.displayBlurImage(context, url, imageView, defRes, blurRadius); } } @Override public void displayBlurImage(Context context, int resId, ImageView imageView, int blurRadius) { if (client != null) { client.displayBlurImage(context, resId, imageView, blurRadius); } } @Override public void displayBlurImage(Fragment fragment, String url, ImageView imageView, int defRes, int blurRadius) { if (client != null) { client.displayBlurImage(fragment, url, imageView, defRes, blurRadius); } } @Override public void displayImageInResource(Context context, int resId, ImageView imageView) { if (client != null) { client.displayImageInResource(context, resId, imageView); } } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView) { if (client != null) { client.displayImageInResource(fragment, resId, imageView); } } @Override public void displayImageInResource(Context context, int resId, ImageView imageView, BitmapTransformation transformations) { if (client != null) { client.displayImageInResource(context, resId, imageView, transformations); } } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, BitmapTransformation transformations) { if (client != null) { client.displayImageInResource(fragment, resId, imageView, transformations); } } @Override public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes) { if (client != null) { client.displayImageInResource(context, resId, imageView, defRes); } } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes) { if (client != null) { client.displayImageInResource(fragment, resId, imageView, defRes); } } @Override public void displayImageInResource(Context context, int resId, ImageView imageView, int defRes, BitmapTransformation transformations) { if (client != null) { client.displayImageInResource(context, resId, imageView, defRes, transformations); } } @Override public void displayImageInResource(Fragment fragment, int resId, ImageView imageView, int defRes, BitmapTransformation transformations) { if (client != null) { client.displayImageInResource(fragment, resId, imageView, defRes, transformations); } } @Override public void displayImageInResourceTransform(Activity activity, int resId, ImageView imageView, Transformation transformation, int errorResId) { if (client != null) { client.displayImageInResourceTransform(activity, resId, imageView, transformation, errorResId); } } @Override public void displayImageInResourceTransform(Context context, int resId, ImageView imageView, Transformation transformation, int errorResId) { if (client != null) { client.displayImageInResourceTransform(context, resId, imageView, transformation, errorResId); } } @Override public void displayImageInResourceTransform(Fragment fragment, int resId, ImageView imageView, Transformation transformation, int errorResId) { if (client != null) { client.displayImageInResourceTransform(fragment, resId, imageView, transformation, errorResId); } } @Override public void displayImageByNet(Context context, String url, ImageView imageView, int defRes, Transformation transformation) { if (client != null) { client.displayImageByNet(context, url, imageView, defRes, transformation); } } @Override public void displayImageByNet(Fragment fragment, String url, ImageView imageView, int defRes, Transformation transformation) { if (client != null) { client.displayImageByNet(fragment, url, imageView, defRes, transformation); } } @Override public void displayImageByNet(Activity activity, String url, ImageView imageView, int defRes, Transformation transformation) { if (client != null) { client.displayImageByNet(activity, url, imageView, defRes, transformation); } } /**尽管及时取消不必要的加载是很好的实践,但这并不是必须的操作。 * 实际上,当 Glide.with() 中传入的 Activity 或 Fragment 实例销毁时, * Glide 会自动取消加载并回收资源。这里我隐藏了api的调用 * {@hide} */ @Override public void clear(Activity activity, ImageView imageView) { if (client != null) { client.clear(activity, imageView); } } /**尽管及时取消不必要的加载是很好的实践,但这并不是必须的操作。 * 实际上,当 Glide.with() 中传入的 Activity 或 Fragment 实例销毁时, * Glide 会自动取消加载并回收资源。这里我隐藏了api的调用 * {@hide} */ @Override public void clear(Context context, ImageView imageView) { if (client != null) { client.clear(context, imageView); } } /**尽管及时取消不必要的加载是很好的实践,但这并不是必须的操作。 * 实际上,当 Glide.with() 中传入的 Activity 或 Fragment 实例销毁时, * Glide 会自动取消加载并回收资源。这里我隐藏了api的调用 * {@hide} */ @Override public void clear(Fragment fragment, ImageView imageView) { if (client != null) { client.clear(fragment, imageView); } } /** * 指定选择哪种缓存的策略 * @param fragment * @param url * @param diskCacheStrategy * @param imageView */ @Override public void displayImageByDiskCacheStrategy(Fragment fragment, String url, DiskCacheStrategy diskCacheStrategy, ImageView imageView) { if (client != null) { client.displayImageByDiskCacheStrategy(fragment, url, diskCacheStrategy, imageView); } } @Override public void displayImageByDiskCacheStrategy(Activity activity, String url, DiskCacheStrategy diskCacheStrategy, ImageView imageView) { if (client != null) { client.displayImageByDiskCacheStrategy(activity, url, diskCacheStrategy, imageView); } } @Override public void displayImageByDiskCacheStrategy(Context context, String url, DiskCacheStrategy diskCacheStrategy, ImageView imageView) { if (client != null) { client.displayImageByDiskCacheStrategy(context, url, diskCacheStrategy, imageView); } } @Override public void disPlayImageOnlyRetrieveFromCache(Fragment fragment, String url, ImageView imageView) { if (client != null) { client.disPlayImageOnlyRetrieveFromCache(fragment, url, imageView); } } @Override public void disPlayImageOnlyRetrieveFromCache(Activity activity, String url, ImageView imageView) { if (client != null) { client.disPlayImageOnlyRetrieveFromCache(activity, url, imageView); } } @Override public void disPlayImageOnlyRetrieveFromCache(Context context, String url, ImageView imageView) { if (client != null) { client.disPlayImageOnlyRetrieveFromCache(context, url, imageView); } } @Override public void disPlayImageSkipMemoryCache(Fragment fragment, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { if (client != null) { client.disPlayImageSkipMemoryCache(fragment, url, imageView,skipflag,diskCacheStratey); } } @Override public void disPlayImageSkipMemoryCache(Activity activity, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { if (client != null) { client.disPlayImageSkipMemoryCache(activity, url, imageView,skipflag,diskCacheStratey); } } @Override public void disPlayImageSkipMemoryCache(Context context, String url, ImageView imageView, boolean skipflag, boolean diskCacheStratey) { if (client != null) { client.disPlayImageSkipMemoryCache(context, url, imageView,skipflag,diskCacheStratey); } } @Override public void disPlayImageErrorReload(Fragment fragment, String url, String fallbackUrl, ImageView imageView) { if (client != null) { client.disPlayImageErrorReload(fragment, url,fallbackUrl, imageView); } } @Override public void disPlayImageErrorReload(Activity activity, String url, String fallbackUrl, ImageView imageView) { if (client != null) { client.disPlayImageErrorReload(activity, url,fallbackUrl, imageView); } } @Override public void disPlayImageErrorReload(Context context, String url, String fallbackUrl, ImageView imageView) { if (client != null) { client.disPlayImageErrorReload(context, url,fallbackUrl, imageView); } } @Override public void disPlayImagedisallowHardwareConfig(Fragment fragment, String url, ImageView imageView) { if (client != null) { client.disPlayImagedisallowHardwareConfig(fragment, url,imageView); } } @Override public void disPlayImagedisallowHardwareConfig(Activity activity, String url, ImageView imageView) { if (client != null) { client.disPlayImagedisallowHardwareConfig(activity, url,imageView); } } @Override public void disPlayImagedisallowHardwareConfig(Context context, String url, ImageView imageView) { if (client != null) { client.disPlayImagedisallowHardwareConfig(context, url,imageView); } } @Override public void disPlayImageProgress(Context context, String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { if (client != null) { client.disPlayImageProgress(context, url,imageView,placeholderResId,errorResId,listener); } } @Override public void disPlayImageProgress(Activity activity, String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { if (client != null) { client.disPlayImageProgress(activity, url,imageView,placeholderResId,errorResId,listener); } } @Override public void disPlayImageProgress(Fragment fragment, String url, ImageView imageView, int placeholderResId, int errorResId, OnGlideImageViewListener listener) { if (client != null) { client.disPlayImageProgress(fragment, url,imageView,placeholderResId,errorResId,listener); } } @Override public void disPlayImageProgressByOnProgressListener(Context context, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { if (client != null) { client.disPlayImageProgressByOnProgressListener(context, url,imageView,placeholderResId,errorResId,onProgressListener); } } @Override public void disPlayImageProgressByOnProgressListener(Activity activity, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { if (client != null) { client.disPlayImageProgressByOnProgressListener(activity, url,imageView,placeholderResId,errorResId,onProgressListener); } } /** * 需要监听 总的字节数,和文件的大小,同时也可以扩展为,加载本地图片 * @param fragment * @param url * @param imageView * @param placeholderResId * @param errorResId * @param onProgressListener */ @Override public void disPlayImageProgressByOnProgressListener(Fragment fragment, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener) { if (client != null) { client.disPlayImageProgressByOnProgressListener(fragment, url,imageView,placeholderResId,errorResId,onProgressListener); } } /** * 过渡选项 TransitionOptions 用于决定你的加载完成时会发生什么。 使用 TransitionOption 可以应用以下变换: View淡入 与占位符交叉淡入 或者什么都不发生 从白色 慢慢变透明 5s的间隔 GlideApp.with .load .transition(withCrossFade 可以传入过度的时间 .into(mImageView_7); */ @Override public void displayImageByTransition(Context context, String url, TransitionOptions transitionOptions, ImageView imageView) { if (client != null) { client.displayImageByTransition(context, url,transitionOptions,imageView); } } @Override public void displayImageByTransition(Activity activity, String url, TransitionOptions transitionOptions, ImageView imageView) { if (client != null) { client.displayImageByTransition(activity, url,transitionOptions,imageView); } } @Override public void displayImageByTransition(Fragment fragment, String url, TransitionOptions transitionOptions, ImageView imageView) { if (client != null) { client.displayImageByTransition(fragment, url,transitionOptions,imageView); } } @Override public void glidePauseRequests(Context context) { if (client != null) { client.glidePauseRequests; } } @Override public void glidePauseRequests(Activity activity) { if (client != null) { client.glidePauseRequests; } } @Override public void glidePauseRequests(Fragment fragment) { if (client != null) { client.glidePauseRequests; } } @Override public void glideResumeRequests(Context context) { if (client != null) { client.glideResumeRequests; } } @Override public void glideResumeRequests(Activity activity) { if (client != null) { client.glideResumeRequests; } } @Override public void glideResumeRequests(Fragment fragment) { if (client != null) { client.glideResumeRequests; } } @Override public void displayImageThumbnail(Context context, String url, String backUrl, int thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(context,url,backUrl,thumbnailSize,imageView); } } @Override public void displayImageThumbnail(Activity activity, String url, String backUrl, int thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(activity,url,backUrl,thumbnailSize,imageView); } } @Override public void displayImageThumbnail(Fragment fragment, String url, String backUrl, int thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(fragment,url,backUrl,thumbnailSize,imageView); } } /** * 没有地址也需要指定缩略图 * @param fragment * @param url * @param thumbnailSize * @param imageView */ @Override public void displayImageThumbnail(Fragment fragment, String url, float thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(fragment,url,thumbnailSize,imageView); } } @Override public void displayImageThumbnail(Activity activity, String url, float thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(activity,url,thumbnailSize,imageView); } } @Override public void displayImageThumbnail(Context context, String url, float thumbnailSize, ImageView imageView) { if (client != null) { client.displayImageThumbnail(context,url,thumbnailSize,imageView); } }}

关于图片下载进度的监听,在AppGlideModuleProgress 需求开首化ProgressManager

@GlideModulepublic class AppGlideModuleProgress extends AppGlideModule { /** OkHttp 是一个底层网络库(相较于 Cronet 或 Volley 而言),尽管它也包含了 SPDY 的支持。 OkHttp 与 Glide 一起使用可以提供可靠的性能,并且在加载图片时通常比 Volley 产生的垃圾要少。 对于那些想要使用比 Android 提供的 HttpUrlConnection 更 nice 的 API, 或者想确保网络层代码不依赖于 app 安装的设备上 Android OS 版本的应用,OkHttp 是一个合理的选择。 如果你已经在 app 中某个地方使用了 OkHttp ,这也是选择继续为 Glide 使用 OkHttp 的一个很好的理由,就像选择其他网络库一样。 添加 OkHttp 集成库的 Gradle 依赖将使 Glide 自动开始使用 OkHttp 来加载所有来自 http 和 https URL 的图片 * @param context * @param glide * @param registry */ @Override public void registerComponents(Context context, Glide glide, Registry registry) { super.registerComponents(context, glide, registry); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getOkHttpClient; }}

ProgressManager

public class ProgressManager { private static List<WeakReference<OnProgressListener>> listeners = Collections.synchronizedList(new ArrayList<WeakReference<OnProgressListener>>; private static OkHttpClient okHttpClient; private ProgressManager() { } public static OkHttpClient getOkHttpClient() { if (okHttpClient == null) { okHttpClient = new OkHttpClient.Builder() .addNetworkInterceptor(new Interceptor() { @Override public Response intercept(@NonNull Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed; return response.newBuilder() .body(new ProgressResponseBody(request.url().toString(), response.body(), LISTENER)) .build .build(); } return okHttpClient; } private static final OnProgressListener LISTENER = new OnProgressListener() { @Override public void onProgress(String imageUrl, long bytesRead, long totalBytes, boolean isDone, GlideException exception) { if (listeners == null || listeners.size return; for (int i = 0; i < listeners.size { WeakReference<OnProgressListener> listener = listeners.get; OnProgressListener progressListener = listener.get(); if (progressListener == null) { listeners.remove; } else { progressListener.onProgress(imageUrl, bytesRead, totalBytes, isDone, exception); } } } }; public static void addProgressListener(OnProgressListener progressListener) { if (progressListener == null) return; if (findProgressListener(progressListener) == null) { listeners.add(new WeakReference<>(progressListener)); } } public static void removeProgressListener(OnProgressListener progressListener) { if (progressListener == null) return; WeakReference<OnProgressListener> listener = findProgressListener(progressListener); if (listener != null) { listeners.remove; } } private static WeakReference<OnProgressListener> findProgressListener(OnProgressListener listener) { if (listener == null) return null; if (listeners == null || listeners.size return null; for (int i = 0; i < listeners.size { WeakReference<OnProgressListener> progressListener = listeners.get; if (progressListener.get() == listener) { return progressListener; } } return null; }}

ProgressResponseBody

public class ProgressResponseBody extends ResponseBody { private String imageUrl; private ResponseBody responseBody; private OnProgressListener progressListener; private BufferedSource bufferedSource; public ProgressResponseBody(String url, ResponseBody responseBody, OnProgressListener progressListener) { this.imageUrl = url; this.responseBody = responseBody; this.progressListener = progressListener; } @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() { return responseBody.contentLength(); } @Override public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source; } return bufferedSource; } private Source source(Source source) { return new ForwardingSource { long totalBytesRead = 0; @Override public long read(@NonNull Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead += (bytesRead == -1) ? 0 : bytesRead; if (progressListener != null) { progressListener.onProgress(imageUrl, totalBytesRead, contentLength(), (bytesRead == -1), null); } return bytesRead; } }; }}

OnProgressListener

public interface OnProgressListener { /** * * @param imageUrl 图片地址 * @param bytesRead 下载了多少字节 * @param totalBytes 总共的大小 * @param isDone 是否完成 * @param exception 异常 */ void onProgress(String imageUrl, long bytesRead, long totalBytes, boolean isDone, GlideException exception);}

OnGlideImageViewListener

public interface OnGlideImageViewListener { /** * * @param percent 下载进度的百分比,不关心,大小 * @param isDone 是否完成 * @param exception 异常 */ void onProgress(int percent, boolean isDone, GlideException exception);}

glide4.X优点1、新的文书档案,客商能够经过付出央浼到Glide’s gh-pages分支进献。2、用户能够增多新品类或自定义选项集来轻易地自定义Glide通畅的API。大量简化个人乞求类型,确定保证选拔万法归宗,易于使用,固然你正在加载差异等级次序的财富。3、种种品质订正,包含在下载采样图像时大量调减污源,尤其智能的暗中认可磁盘缓存计策,甚至加载GIF时品质升高。4、修改了视图大小和布局的拍卖,特别是在RecyclerView中。官方文书档案:https://muyangmin.github.io/glide-docs-cn/

Glide V4变化极大的是库管理选项的法子。在Glide v3中,选项是由一体系复杂的多类型构建器单独管理的。在Glide v4中,这几个已被抱有单一类型的单个构建器和能够提要求创设器的一多级选项的目的所代替。Glide 生成的API通过将甄挑对象和别的带有的集成库与创设器的选项联合,来创建单个通畅的API。 能够简简单单明了为,Glide v4 将Glide V3中Glide.with(State of Qatar完毕的一密密层层复杂功效拆分成一些单独的API。

提出看官方文书档案,固然供给点时间!

本篇借鉴android开采情势索求和android群英传计算出来,方便温馨事后翻看使用。达成View滑动的多样艺术,在看这一段的时候发掘自个儿的底子相当糟糕,相当多从前都未曾弄通晓。接下来大家就详细的讲一讲。首要的思绪都以当触摸view的时候,记录下来当前触摸点的坐标,当手提式有线电话机活动的时候,记录下来移动后的触动的坐标,进而得出偏移量,使用那些偏移量来持续的改造view的坐标,进而完毕滑动的经过。

本篇文章首要记录:在档案的次序中合拢Wechat支付和Wechat支付的短平快应用方案

近年来看到了前不久头条的tablayout,认为极其新鲜,也正如感兴趣,效果就是底下那几个

最后总计下Glide4.X相比较Glide3.X的区分:
  • 使用layout的方法;
  • 使用offsetLeftAndRight()与offsetTopAndBottom();
  • 使用LayoutParams();
  • 使用scrollTo()与scrollBy();
  • 使用Scroller类
  • 利用性质动漫

遨易科学技术支出插件

www.64222.com 6gif5新文件.gif

1、选项

Glide v4 中的三个超级大的校正是Glide库管理选项(centerCrop(), placeholder() 等卡塔尔国的点子。在 v3 本子中,选项由一密密层层复杂的异营造造者(multityped buildersState of Qatar单独管理。在新本子中,由三个单一类型的独一二个建造者接管一应有尽有选项对象。Glide 的generated API进一层简化了那一个操作:它会联合传入建造者的取舍对象和任何已盈盈的集成Curry的选项,以生成二个流畅的 API。

先是大家要介绍一下得到坐标的点子

View 提供的诀要

  1. getTop(卡塔尔国:获取到的是View自己的顶边到父控件顶边的离开
  2. getLeft(State of Qatar:获取到的是View本身的右边手到父控件左侧的间隔
  3. getBottom(卡塔尔国:获取到的是View自个儿底边到父控件顶边的相距
  4. getRight(卡塔尔(قطر‎:获取到的是View本人右侧到父控件侧面的相距

Montion伊夫nt提供的方式

  1. getX(卡塔尔:获取点击事件间距自身控件侧边的间隔
  2. getY(State of Qatar:获取点击事件间距自个儿控件顶边的相距
  3. getRawX(State of Qatar: 获取点击事件间隔整个荧屏左侧的偏离
  4. getRawY(State of Qatar:获取点击事件间隔整个荧屏顶边的离开

应用onLayout的主意可以试着view的岗位,那么大家就足以因此转移left,right,top,bottom来使View移动,结合MontionEvent来兑现view跟发轫指的滑动移动。

 @Override public boolean onTouchEvent(MotionEvent event) { //使用getx,getY实现view移动A:// int x =  event.getX();// int y =  event.getY(); //使用getrawx,rawY,实现移动; int rawX =  event.getRawX(); int rawY =  event.getRawY(); switch (event.getAction{ case MotionEvent.ACTION_DOWN:B:// lastX = x;// lastY = y;/ lastX = rawX; lastY = rawY; break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE:C:// int offsetX = x - lastX;// int offsetY = y - lastY; int offsetX = rawX-lastX; int offsetY = rawY-lastY; // layout+offsetX,getTop()+offsetY,getRight() // +offsetX,getBottom()+offsetY);// layout+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);D: lastX = rawX; lastY = rawY; break; } return true; }

此间最首要分使用三种方法一种是getX,getY, 。一种是getRawX,getRawY来得到触摸点的坐标A:每回调用onTouch伊芙nt的时候都来获得一下触摸点的坐标B:在按下的时候记录一下触摸点的坐标C:在move的风浪的时候进行,进行偏移量的计量。D:这里有有些不相符正是运用getRawX,和getRawY的时候须要最终再一次安装开头坐标,因为他相对于显示屏来说的,所以每便都需求再度安装赋值。

内部集成了支付宝支出和Wechat支付 一分钟集成集成开采不是梦 现在仅协助支付宝和Wechat 之后将合併银行职员联合会等支付办法

像这种效果与利益应该是供给自定义的View来完毕的,能够观望在滑行进程中,多个相邻的tab是有一部分颜色的转移的,前三个tab部分复苏成中灰,后叁个tab会有的改为棕红,那决议于滑动的间距.

2、RequestBuilder

对于这类方法:

listener()thumbnailinto()

在 Glide v4 版本中,只设有五个 RequestBuilder 对应多少个您正在试图加载的档期的顺序(Bitmap, Drawable, GifDrawable 等)。 RequestBuilder 能够一贯访谈对这几个加载进程有影响的选项,饱含你想加载的数据模型(url, uri等),或许存在的缩略图号令,以致其余的监听器RequestBuilder也是你使用 into() 或者 preload() 方法开端加载的地方:

RequestBuilder<Drawable> requestBuilder = Glide.with .load;requestBuilder .thumbnail(Glide.with .load(thumbnailUrl)) .listener(requestListener) .load .into(imageView);
此处有个为题正是为啥接纳getX,getY不用再每一趟move事件截至的时候再一次赋值开始值,而getRawX和getRawY用,那就是因为相没有错基准不相同,getX和getY是相对于本人的,而getRawX,getRawY是周旋于荧屏的。这下掌握了啊

和Layout方法相像,都以对左上右下移动的叁个装进,我们只需给计算出来的偏移量给她就行offsetLeftAndRight(State of Qatar是对左右的一个封装offsetTopAndBottom(卡塔尔国是对上下的多个包装

我们得以利用LayoutParams的措施来促成view的移位,LayoutParams能够设置view的职务,也能够由此geLayoutParams方法来获得当前view的职位。自然也足以行使setLayoutParams方法来修正View的地点,结合Touch伊夫nt方法就能够让View举行转移。a: 获取偏移量的法子和前八个一律,b:这里的LayoutParams,是基于父类的项目来定的。是 LinearLayout.LayoutParams ,依旧RelativeLayout.LayoutParams 。。。等

 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLeft()+offsetX; layoutParams.topMargin = getTop()+offsetY; setLayoutParams(layoutParams);

先是介绍一下scrollTo和scrollBy的不二等秘书籍scrollTo:具体的一个坐标,是相对滑动都以以庐山真面目目坐标为起初坐标来测算的。scrollBy:那几个源码中能够见到,scrollBy调用的是scrollTo,是争执滑动,是八个间距值。

www.64222.com 7根build.gradlewww.64222.com 8gradle库导入www.64222.com 9支付宝支付www.64222.com 10Wechat支付

先是每贰个tab应该都是自定义的View,因为那涉及到了部分文字变色,所以理应先定制一个可见部分文字变色的View,普通的View当然不扶助啊~

3、诉求选项

对于那类方法:

centerCrop()placeholderpriority()diskCacheStrategy()

大部取舍被活动到了一个独门的叫做 RequestOptionswww.64222.com, 的目的中,

RequestOptions options = new RequestOptions() .centerCrop() .placeholder(R.drawable.placeholder) .error(R.drawable.error) .priority(Priority.HIGH);

RequestOptions 允许你一遍钦命一多种的选项,然后对多个加载重用它们:

RequestOptions myOptions = new RequestOptions() .fitCenter() .override;Glide.with .load .apply(myOptions) .into(drawableView);Glide.with .asBitmap() .apply(myOptions) .load .into(bitmapView);
scrollTo,ScrollBy移动的是View的开始和结果。

那边譬释尊说学一下怎么scrollTo和AScrollBy的参数的正负难题

要是有四个View,大家明天想把View从活动到。这里的坐标都以指的View的左上角的坐标那么偏移量正是到的间隔,即-=(-100,-100)。那么便是scrollTo(-100,-100)

只要大家今后想把View从,移动到那么偏移量正是从到的偏离,即-=。

新建目录与公事如下

先来讲下思路~首要用的不二等秘书诀是canvas的clipRect方法,先来看下那个情势啥子意思哟..

4、变换

Glide v4 里的 Transformations 现在会交替此前设置的别样调换。在 Glide v4 中,假设您想使用超越多个的 Transformation,你要求利用 transforms() 方法:

Glide.with .load .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners .into;

或使用 generated API:

GlideApp.with .load .transforms(new CenterCrop(), new RoundedCorners .into;
大家前几天想通过这两种办法来落到实处View的滑动
  • getParent.scrollTo(-(lastX-100),-(lastY-100));
  • getParent.scrollBy(-offsetX,-offsetY);

www.64222.com 11目录

/** * Intersect the current clip with the specified rectangle, which is * expressed in local coordinates. * * @param left The left side of the rectangle to intersect with the * current clip * @param top The top of the rectangle to intersect with the current clip * @param right The right side of the rectangle to intersect with the * current clip * @param bottom The bottom of the rectangle to intersect with the current * clip * @return true if the resulting clip is non-empty */ public boolean clipRect(int left, int top, int right, int bottom) { return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); }
5、解码格式

在 Glide v3, 默认的 DecodeFormatDecodeFormat.PREFER_RGB_565,它将接纳 [Bitmap.Config.RGB_565],除非图片富含或可能带有透明像素。对于给定的图片尺寸,RGB_565 只使用 [Bitmap.Config.ARGB_8888] 二分之一的内存,但对于特定的图形有真相大白的画申斥题,包罗条纹和设色。为了防止RGB_565的画责备题,Glide 现在私下认可使用 ARGB_8888。结果是,图片性能变高了,但内部存款和储蓄器使用也加进了。

要将 Glide v4 默认的 DecodeFormat 改回 DecodeFormat.PREFER_RGB_565,请在 AppGlideModule 中选取三个 RequestOption

@GlideModulepublic final class YourAppGlideModule extends GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)); }}

至于接纳 AppGlideModules 的越来越多音讯,请查阅 配置 页面。请在意,为了让 Glide 开采你的 AppGlideModule 完成,你必得保障加多了对 Glide 的批注深入剖析器的凭仗。关于怎样设置那一个库的越来越多信息,请查看 下载和安装。

行使Scroller类能够实现View平滑的移动作效果果,也是运动的View的原委。

利用Scroller有八个步骤:A: Scroller mScroller = new Scroller;B:重写computeScroll(卡塔尔;差不离模板都以一律的

 @Override public void computeScroll() { super.computeScroll(); if (mscroller.computeScrollOffset{ getParent.scrollTo(mscroller.getCurrX(),mscroller.getCurrY; invalidate(); } }

C:调用scroller.startScroll方法。这里要介意一个点正是startScroll这几个办法,源码中只是对气象地方的一个赋值,并从未让View进行滑动,所以大家要调用刷新的秘技invalidate(),来让他刷新ondraw,在ondraw方法中会调用computeScroll方法,那样就能够让view举办活动了。

 case MotionEvent.ACTION_UP: View parent =  getParent(); mscroller.startScroll(parent.getScrollX(),parent.getScrollY(),-parent.getScrollX(),-parent.getScrollY; invalidate(); break;

以此就超少讲了,动漫自己就是一个让View渐变的二个进程。

类详细情形如下

解释一下,里边的多个参数裁剪范围的左上右下的岗位,比较好通晓,供给静心的是,使用完那个点子后供给立刻的上升绘制范围,所以总体代码如下

6、过渡选项

对此那类方法:

crossFade()animate()

支配从占位符到图片和/或缩略图到全图的穿插淡入和任何类别转换的选项,被挪动到了 TransitionOptions 中。

要采纳接入,请使用下列选项中切合您央浼的财富类型的五个:

  • GenericTransitionOptions
  • DrawableTransitionOptions
  • BitmapTransitionOptions

只要你想移除任何暗中认可的联网,能够行使 TransitionOptions.dontTransition()]17 。

连片动漫通过 RequestBuilder 应用到央浼上:

Glide.with .load .transition(withCrossFade(R.anim.fade_in, 300));
下面是二种达成弹性滑动的主意,介绍中关键传授落成的思绪,其实还大概有众多主意可以达成,比容使用延缓的章程,能够让handler发送延迟音讯,再调用scrollerTo的措施,来达成View的滑行,等等吧,大家要灵活运用,不断揣摩和扩展来得以完结和睦想要的功用!

www.64222.com 12类详情

canvas.save(); canvas.clipRect(left, top, right, bottom); //再做绘制操作例如本片要用到的drawText()canvas.restore(); 
友情链接: 网站地图
Copyright © 2015-2019 http://www.nflfreepicks.net. 新葡萄京娱乐场网址有限公司 版权所有