Android杂谈(19)Glide实践

来源:互联网 发布:任我行软件txyapp 编辑:程序博客网 时间:2024/06/03 18:29

转载请注意:http://blog.csdn.net/wjzj000/article/details/53010614


本菜开源的一个自己写的Demo,希望能给Androider们有所帮助,水平有限,见谅见谅… 
https://github.com/zhiaixinyang/PersonalCollect (拆解GitHub上的优秀框架于一体,全部拆离不含任何额外的库导入) 
https://github.com/zhiaixinyang/MyFirstApp(Retrofit+RxJava+MVP)





(新增内容:过渡动画,以及自定义动画,简单的跳过缓存)

今天来记录一下关于Glide的使用,已经其中遇到的一些小问题。

首先是Gradle....

compile 'com.github.bumptech.glide:glide:3.7.0'
其次就是Glide的应用:

Glide.with(context)        .load(url)        .placeholder(R.mipmap.no_avatar)        .error(R.mipmap.error_photo)        .override(200,200)        .fitCenter()        .into(imageView);

首先是with()传入一个Context,然后就是load()可以传入url等等...placeholder是指当网络图片没有加载完毕显示的图片。error()表示加载网络数据出错,显示的图片。override()则是剪裁显示。fitCenter()填充控件。into()在那个控件里进行显示。

这种使用方式会遇到问题!!不知道是bug还是什么原因。如果你的页面需要加载很多图片是,有时会出现一些图片并没有加载出来。只有不断的刷新才会逐渐刷新出来。


如何解决这个问题?

我用的方式是不使用placeholder()和error()方法。这样,就可以正常一次性全部加载出来并正常显示。当然这种方式很尴尬,因为不能使用占位图和错误图片....


当然我们的glide也可以用其他的方式加载图片。

比如在load中也可以传int值也就是R.drawable/R.mipmap中我们引入的项目文件。

而且也支持传入一个Flie对象....

并且还有byte[]的值,当然这里有些小问题那就是正常情况下我们可以不怎么好获取byte[]类型的数据。这里我提供这个小小的解决方法:

public static byte[] getByteFromBitmap(Bitmap bitmap){        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);        byte[] bytes=baos.toByteArray();        return bytes;    }
如果想要反过来使用呢?

public static Bitmap getBitmapFromByte(byte[] datas){        if (datas.length!=0){            return BitmapFactory.decodeByteArray(datas,0,datas.length);        }else{            return null;        }    }






不过,Glide真的很强大。上文的这几行代码,也可以直接加载gif并且正常在ImageView中显示!并且不需要任何其他引入!!


11月29日补充:

这里补充几个额外的用法:

Glide有着非常强大的拓展性。比如下边这个直接用Glide处理圆角效果。先继承BitmapTransformation,重写相应的方法。完成你想要处理的业务逻辑。

public class GlideCircleTransform extends BitmapTransformation {    public GlideCircleTransform(Context context) {        super(context);    }    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {        return circleCrop(pool, toTransform);    }    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {        if (source == null) return null;        int size = Math.min(source.getWidth(), source.getHeight());        int x = (source.getWidth() - size) / 2;        int y = (source.getHeight() - size) / 2;        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);        if (result == null) {            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);        }        Canvas canvas = new Canvas(result);        Paint paint = new Paint();        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));        paint.setAntiAlias(true);        float r = size / 2f;        canvas.drawCircle(r, r, r, paint);        return result;    }    @Override public String getId() {        return getClass().getName();    }}
然后通过Glide的transform方法,传递进去:

Glide.with(context).load(url).transform(new GlideCircleTransform(context)).into(iv);
效果如下:

当然还有一些比较大众的用法:

内置的淡入淡出的过度动画(持续一秒):

.crossFade(1000)

当然动画也可以自定义:

.animate(new ViewPropertyAnimation.Animator() {    @Override    public void animate(View view) {        new ObjectAnimator().ofFloat(view,"translationX",0,100).setDuration(1000).start();    }}
在重写的animate方法中,自己写了一个属性动画。运行时就是这个ImageView做出了对应的动画效果。

跳过内存缓存:

.skipMemoryCache(true)
不进行硬盘缓存:关于其他缓存模式比较好理解很多博客也都有涉及所以这个不再累述。

.diskCacheStrategy(DiskCacheStrategy.NONE)


先记录到此,以后遇到实用的就继续加上。

最后希望各位看官可以star我的GitHub,三叩九拜,满地打滚求star: 
https://github.com/zhiaixinyang/PersonalCollect 
https://github.com/zhiaixinyang/MyFirstApp


1 0
原创粉丝点击