最新Android Glide4.0加载Gif图片到ImageView
在我过去的文章里面,介绍了旧的Glide 3.0+的加载Gif图片方式(见文章:http://blog.csdn.net/zhangphil/article/details/45561983 ),现在Glide 4.0相较于Glide 3.0+发生了很大变化,现在给出一个新版Glide 4.0加载Gif图片的代码例子。
package zhangphil.phildemo;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.image);
load(image);
}
private void load(ImageView image) {
RequestOptions options = new RequestOptions()
.centerCrop()
//.placeholder(R.mipmap.ic_launcher_round)
.error(android.R.drawable.stat_notify_error)
.priority(Priority.HIGH)
//.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
Glide.with(this)
.load(R.drawable.loading)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.apply(options)
//.thumbnail(Glide.with(this).load(R.mipmap.ic_launcher))
.into(image);
}
}