Android知识Android技术知识首页投稿(暂停使用,暂停投稿)

初探Glide,Google推介的图片缓存库

2017-02-16  本文已影响265人  CokeNello

0 .Thanks

项目地址
Android Glide图片加载(加载监听、加载动画)
【Glide】重新加载图片

---导入:2017年2月15日19:06:06

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

1 .基本用法

Glide
    .with( context )
    .placeholder(R.drawable.loading)
    .error(R.drawable.failed)
    .load( eatFoodyImages[0] )
    .into( imageViewInternet );

2 .设置其缓存策略

在默认情况下,例如上面的请求,Glide默认开启:磁盘缓存,内存缓存。
我们可以控制这个策略:

Glide
    .with( context )
    .placeholder(R.drawable.loading)
    .error(R.drawable.failed)
    .skipMemoryCache( true )
    .diskCacheStrategy( DiskCacheStrategy.NONE )
    .load( eatFoodyImages[0] )
    .into( imageViewInternet );

3 .动画

ViewPropertyAnimation.Animator animationObject = new ViewPropertyAnimation.Animator() {  
    @Override
    public void animate(View view) {
        // if it's a custom view class, cast it here
        // then find subviews and do the animations
        // here, we just use the entire view for the fade animation
        view.setAlpha( 0f );

        ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view, "alpha", 0f, 1f );
        fadeAnim.setDuration( 2500 );
        fadeAnim.start();
    }
};

然后再Glide中设置: .animate( animationObject )

4 .加载完成监听

Glide.with(ShowImgActivity.this)
     .load(urlString)
     .centerCrop()
     .error(R.drawable.failed)
     .crossFade()
     .into(new GlideDrawableImageViewTarget(imageView) {
        @Override
        public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
            super.onResourceReady(drawable, anim);
            //在这里添加一些图片加载完成的操作
        }
    )};

5 .加载优先级

在Glide的内部,图片的加载可以变得有优先级:
通过:.priority( Priority.HIGH )设置

6 .其他的小问题

1)Glide去加载头像:
上一篇 下一篇

猜你喜欢

热点阅读