图片加载框架Glide

本人以前就是网络加载图片各类费劲,并且还有另开线程,很是麻烦,代码量多且复杂,后来接触到图片加载框架Glide,发现太好用了,支持各类格式还有GIF,支持本地和网络等加载,只要几行代码,完美解决,并且性能很好,还有它的扩展框架glide-transformations能够作各类效果。这里分享给你们,须要的能够看一下。java

图片加载框架Glide与glide-transformations(能够实现模糊以及圆角各类效果,为Glide的扩展框架)android

glide-transformations的git地址:https://github.com/wasabeef/glide-transformationsgit

Glide的git的项目地址:https://github.com/bumptech/glide/github

Glide的jar的下载地址:https://github.com/bumptech/glide/releases缓存

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile files('libs/glide-3.7.0-javadoc.jar')
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'jp.wasabeef:glide-transformations:2.0.1'
}

如上设置既可以使用连个库,compile files('libs/glide-3.7.0-javadoc.jar')为本地的Glide的jar包,须要下载。也能够使用:网络

compile 'com.github.bumptech.glide:glide:3.7.0'

使用示例:app

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = "http://easyread.ph.126.net/ZDOGCSZEMvM9jDyF41wifg==/7917045570430793200.jpg";
        ImageView view = (ImageView) findViewById(R.id.iv);
        Glide.with(getApplicationContext()) // 指定Context
                .load("http://i1.wp.com/www.mydesy.com/wp-content/uploads/2013/02/20130211a3.gif?resize=750%2C400")// 指定图片的URL
                .placeholder(R.mipmap.ic_launcher)// 指定图片未成功加载前显示的图片
                .error(R.mipmap.ic_launcher)// 指定图片加载失败显示的图片
                .override(300, 300)//指定图片的尺寸
                .fitCenter()//指定图片缩放类型为fitCenter
                .centerCrop()// 指定图片缩放类型为centerCrop
                .skipMemoryCache(true)// 跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE)//跳过磁盘缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)//仅仅只缓存原来的全分辨率的图像
                .diskCacheStrategy(DiskCacheStrategy.RESULT)//仅仅缓存最终的图像
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全部版本的图像
                .priority(Priority.HIGH)//指定优先级.Glide 将会用他们做为一个准则,并尽量的处理这些请求,可是它不能保证全部的图片都会按照所要求的顺序加载。优先级排序:IMMEDIATE > HIGH > NORMAL > LOW
                .into(view);//指定显示图片的ImageView


        ImageView image2 = (ImageView) findViewById(R.id.iv2);
        Glide.with(this).load(url).bitmapTransform(new CropCircleTransformation(this)).crossFade(1000).into(image2);

        //原图的毛玻璃、高斯模糊效果
        ImageView image3 = (ImageView) findViewById(R.id.iv3);
        Glide.with(this).load(url).bitmapTransform(new BlurTransformation(this, 25)).crossFade(1000).into(image3);

        //原图基础上复合变换成圆图 +毛玻璃(高斯模糊)
        ImageView image4 = (ImageView) findViewById(R.id.iv4);
        Glide.with(this).load(url).bitmapTransform(new BlurTransformation(this, 25), new CropCircleTransformation(this)).crossFade(1000).into(image4);

        //原图处理成圆角,若是是四周都是圆角则是RoundedCornersTransformation.CornerType.ALL
        ImageView image5 = (ImageView) findViewById(R.id.iv5);
        Glide.with(this).load(url).bitmapTransform(new RoundedCornersTransformation(this, 30, 0, RoundedCornersTransformation.CornerType.BOTTOM)).crossFade(1000).into(image5);
    }
}

效果图:框架