圖片加載框架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);
    }
}

效果圖:框架

相關文章
相關標籤/搜索