Watch my line of code take off, Glide loading gif optimization practice

insert image description here

foreword

Recently, gif animations have been used in the project, and the Glide (supporting gif) library has been introduced, so I use Glide to load it; but I still encountered many difficulties in the process of using it. I record it here, hoping to give you some thoughts and suggestions when you encounter similar problems.

img

1. Glide loads gif

1. Add dependencies to the project

dependencies {
    
    
 compile 'com.github.bumptech.glide:glide:4.0.0'
 compile 'com.android.support:support-v4:25.3.1'
}

Glide needs to depend on Support Library v4

2. Load pictures

// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
    
    
 ...
 ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

 Glide.with(this)
.load("http://goo.gl/gEgYUd")
.into(imageView);
}
// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
    
    
 final ImageView myImageView;
 if (recycled == null) {
    
    
 myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
 } else {
    
    
 myImageView = (ImageView) recycled;
 }

 String url = myUrls.get(position);

 Glide
 .with(myFragment)
 .load(url)
 .centerCrop()
 .placeholder(R.drawable.loading_spinner)
 .into(myImageView);

 return myImageView;
}
//不使用它的默认动画
Glide.with(mContext)
 .load(url) 
 .dontAnimate()
 .placeholder(R.drawable.loading_spinner)
 .into(circleImageview);

Tip: Do not use Glide to load images in non-main threads. If you really use it, please replace the context parameter with getApplicationContext

3. Load GIF

If you just simply load gif, it is actually the same as loading a normal picture

Glide.with(this).load(mGifUrl).placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);

If you want to load only the first frame of the gif when loading the gif, and load the gif as a normal picture, you only need to add the asBitmap method

Glide.with(this).load(mGifUrl).asBitmap().placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);

If you want to load only gif, and if it is not gif, the wrong picture will be displayed, then just add the asGif method

 Glide.with(this).load(mGifUrl).asGif().placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);

Many articles on the Internet are copied from a translation, which says that Glide can load local videos, but the translation misses a sentence. Glide will only load the first frame of the local video, which is the thumbnail. In fact, there is no need to convert the Uri when loading the thumbnail, just throw the file into it.

 mVideoFile = new File(Environment.getExternalStorageDirectory(), "xiayu.mp4");

 Glide.with(this).load(mVideoFile).placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);

In most cases, when you use diskCacheStrategy (DiskCacheStrategy.SOURCE), the loading speed of Gif will be significantly improved (in fact, it is to cache gif resources to disk) -> it can solve the situation that gif loads slowly or cannot be loaded (NONE means no data is cached, SOURCE is the cache prototype, original image)

Glide.with(this).load(mGifUrl).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.mipmap.place)
.error(R.mipmap.icon_photo_error)
.into(mIv);

Dynamically display the number of gif images, for example, if I want to display a gif once, it will stop

Glide.with(this).load("url").diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new GlideDrawableImageViewTarget(iv, 1)); 

The GlideDrawableImageViewTarget(ImageView view, int maxLoopCount) method here, maxloopCount can control the display times.

img

Two, Glide loading gif optimization

1. Solve the problem that Glide loads Gif very slowly

 Glide.with(MainActivity.this).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView); 

Add a cache strategy for it, where the cache strategy can be: Source and None, None and no cache, Source cache prototype. If it is ALL and Result, it will not work

2. Load the first frame:

Glide.with(context).load(gifUrl).asBitmap().into(imageViewGifAsBitmap);

3. Control the number of animations:

Glide.with(this).load(getResource()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imageView, 1));

4. GIF time:

Glide.with(FirstActivity.this)

.load(file)

.asGif()

.fitCenter()

.diskCacheStrategy(DiskCacheStrategy.SOURCE)

.listener(new RequestListener<File, GifDrawable>() {
    
    

@Override

public boolean onException(Exception e, File model, Target<GifDrawable> target, boolean isFirstResource) {
    
    

return false;

}

@Override

public boolean onResourceReady(final GifDrawable resource, File model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    
    

new Thread(new Runnable() {
    
    

@Override

public void run() {
    
    

int duration = 0;

try {
    
    

GifDrawable gifDrawable = (GifDrawable) resource;

GifDecoder decoder = gifDrawable.getDecoder();

for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
    
    

duration += decoder.getDelay(i);

}

mGifAdTime = duration;

} catch (Throwable e) {
    
    

}

}

}).start();

return false;

}

})

.into(mAdImg);

Guess you like

Origin blog.csdn.net/Android23333/article/details/131681886