Android Glide 4.0+使用详解

Android Glide 4.0+使用详解,第1张

概述下载和设置AndroidSDK要求使用最低要求-使用Glide要求SDK版本为API14(IceCreamSandwich)及以上。

下载和设置

AndroID SDK 要求

使用最低要求 - 使用 GlIDe 要求 SDK 版本为 API 14 (Ice Cream SanDWich) 及以上。

编译最低要求 - 编译 GlIDe 要在 SDK 版本为 API 26 (Oreo) 及以上。

jar

你可以直接在 GitHub 下载最新的jar包

Gradle

如果使用 Gradle,可从 Maven Central 或 JCenter 中添加对 GlIDe 的依赖。同样,你还需要添加 AndroID 支持库的依赖。

repositorIEs { mavenCentral() maven { url 'https://maven.Google.com' }}dependencIEs {  compile 'com.github.bumptech.glIDe:glIDe:4.1.1'  annotationProcessor 'com.github.bumptech.glIDe:compiler:4.1.1'}

Kotlin

如果你在 Kotlin 编写的类里使用 GlIDe 注解,你需要引入一个 kapt 依赖,以代替常规的 annotationProcessor 依赖:

dependencIEs { kapt 'com.github.bumptech.glIDe:compiler:4.1.1'}

开始使用

基本用法

加载图片

GlIDe.with(fragment)  .load(myUrl)  .into(imageVIEw);

取消加载图片

GlIDe.with(fragment).clear(imageVIEw);

在RecyclerVIEw 中使用

@OverrIDepublic voID onBindVIEwHolder(VIEwHolder holder,int position) {  String url = urls.get(position);  GlIDe.with(fragment)    .load(url)    .into(holder.imageVIEw);}

加载占位图

加载过程中的占用(Placeholder)

GlIDe.with(fragment) .load(url) .placeholder(R.drawable.placeholder) .into(vIEw);

加载失败后显示的图片(Error)

GlIDe.with(fragment) .load(url) .error(R.drawable.error) .into(vIEw);

图片的转换

GlIDe中的大部分设置项都可以通过 Requestoptions 类和 apply() 方法来应用到程序中。 使用 request options 可以实现(包括但不限于):

占位图(Placeholders) 转换(transformations) 缓存策略(Caching StrategIEs) 组件特有的设置项,例如编码质量,或Bitmap的解码配置等。

加载圆形图片

 GlIDe.with(this)        .load(url)        .apply(Requestoptions.circleCroptransform())        .into(ivTest);

加载图片带淡入淡出的动画效果

 GlIDe.with(this)        .load(url)        .Transition(withCrossFade())        .into(ivTest);

等等很多的转换效果,具体自己可以一个一个试试
缓存

GlIDe的默认缓存策略是autoMATIC,
在磁盘缓存

GlIDeApp.with(fragment) .load(url) .diskCacheStrategy(diskCacheStrategy.ALL) .into(imageVIEw);

仅从缓存加载图片

GlIDeApp.with(fragment) .load(url) .onlyRetrIEveFromCache(true) .into(imageVIEw);

跳过内存缓存

GlIDeApp.with(fragment) .load(url) .skipMemoryCache(true) .into(vIEw);

跳过磁盘缓存

GlIDeApp.with(fragment) .load(url) .diskCacheStrategy(diskCacheStrategy.NONE) .into(vIEw);

跳过所有的缓存

GlIDeApp.with(fragment) .load(url) .diskCacheStrategy(diskCacheStrategy.NONE) .skipMemoryCache(true) .into(vIEw);

清理磁盘的缓存

GlIDe.get(applicationContext).cleardiskCache();

高级用法

加载一个图片为高斯模糊效果

使用方法

复制代码 代码如下:
GlIDe.with(getActivity()).load("http://img1.imgtn.bdimg.com/it/u=594559231,2167829292&fm=27&gp=0.jpg").apply(RequestOptions.bitmapTransform(new GlIDeBlurformation(getActivity()))).into(ivTest);
  

用到的其他工具类

package com.qIEzzi.clinic.chengqi.common.utils;import androID.content.Context;import androID.graphics.Bitmap;import androID.support.annotation.NonNull;import com.bumptech.glIDe.load.engine.bitmap_recycle.BitmapPool;import com.bumptech.glIDe.load.resource.bitmap.Bitmaptransformation;import java.security.MessageDigest;/** * Created by yukuoyuan on 2017/9/29. */public class GlIDeBlurformation extends Bitmaptransformation {  private Context context;  public GlIDeBlurformation(Context context) {    this.context = context;  }  @OverrIDe  protected Bitmap transform(@NonNull BitmapPool pool,@NonNull Bitmap totransform,int outWIDth,int outHeight) {    return BlurBitmapUtil.instance().blurBitmap(context,totransform,20,outWIDth,outHeight);  }  @OverrIDe  public voID updatediskCacheKey(MessageDigest messageDigest) {  }}
package com.qIEzzi.clinic.chengqi.common.utils;import androID.annotation.TargetAPI;import androID.content.Context;import androID.graphics.Bitmap;import androID.os.Build;import androID.renderscript.Allocation;import androID.renderscript.Element;import androID.renderscript.RenderScript;import androID.renderscript.ScriptIntrinsicBlur;/** * Created by yukuoyuan on 2017/9/29. */public class BlurBitmapUtil {  private static BlurBitmapUtil sInstance;  private BlurBitmapUtil() {  }  public static BlurBitmapUtil instance() {    if (sInstance == null) {      synchronized (BlurBitmapUtil.class) {        if (sInstance == null) {          sInstance = new BlurBitmapUtil();        }      }    }    return sInstance;  }  /**   * @param context  上下文对象   * @param image   需要模糊的图片   * @param outWIDth 输入出的宽度   * @param outHeight 输出的高度   * @return 模糊处理后的Bitmap   */  @TargetAPI(Build.VERSION_CODES.JELLY_BEAN_MR1)  public Bitmap blurBitmap(Context context,Bitmap image,float blurRadius,int outHeight) {    // 将缩小后的图片做为预渲染的图片    Bitmap inputBitmap = Bitmap.createScaledBitmap(image,outHeight,false);    // 创建一张渲染后的输出图片    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);    // 创建RenderScript内核对象    RenderScript rs = RenderScript.create(context);    // 创建一个模糊效果的RenderScript的工具对象    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,Element.U8_4(rs));    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去    Allocation tmpIn = Allocation.createFromBitmap(rs,inputBitmap);    Allocation tmpOut = Allocation.createFromBitmap(rs,outputBitmap);    // 设置渲染的模糊程度,25f是最大模糊度    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {      blurScript.seTradius(blurRadius);    }    // 设置blurScript对象的输入内存    blurScript.setinput(tmpIn);    // 将输出数据保存到输出内存中    blurScript.forEach(tmpOut);    // 将数据填充到Allocation中    tmpOut.copyTo(outputBitmap);    return outputBitmap;  }}

具体其他效果就自己写吧,其实原理很简单,就是通过继承Bitmaptransformation接口,然后在里边把bitmap处理为自己想要的效果.没有什么过于复杂的过程.

参考资料 : Glide官方文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:Android基于Glide v4.x的图片加载进度监听 总结

以上是内存溢出为你收集整理的Android Glide 4.0+使用详解全部内容,希望文章能够帮你解决Android Glide 4.0+使用详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1149688.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存