Glide for Android (image loading)

Keywords: Android network github Google

1. Add dependency

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

2. Usage

ImageView imageView = (ImageView)findViewById(R.id.imageView)
Glide.with(this)
    .load(url)//Picture address
    .crossFade()//Open animation
    //.dontAnimate()//Close animation
    .placeholder(R.drawable.image1)//The image displayed before loading
    .error(R.drawable.image2)//Pictures displayed after picture loading failure
    .override(width,height)//Change the size of the displayed picture in px
    .priority (Priority.HIGH )//The priority of image loading, and the settings (immediate, high, normal, low) when loading images at the same time are not strictly followed
    .into(imageView);//Load controls for pictures

Thumbnail: (mode 1)

.thumbnail( 0.2f )//Load thumbnail of image (0.2 represents 20% of original image)

Thumbnail: (mode 2)

DrawableRequestBuilder<String> thumbnailRequest = Glide.with( context ).load( url );
    Glide.with( context )
        .load( url )
        .thumbnail( thumbnailRequest )
        .into( imageView );

Show gif and video

gif:

Glide.with(this)
    .load(gifUrl)
    .asGif()
    //.asBitmap()//Display the first frame of gif, i.e. static graph
    .placeholder(R.drawable.default)
    .error(R.drawable.image2)
    .into(imageView);

video: (local)

Glide.with(this)
    .load(Uri.fromFile(new File(filePath)))
    .into(imageView);

It is said that using Glide to load a larger gif will cause a jam. Loading a local image can't be set to complete listening, so you can use Android gif drawable library to load it.

Add Android GIF drawable dependency:

compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.10'

How to use: (local picture)

GifImageView gifImageView = (GifImageView) findViewById(R.id.gifImageView);
GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();

gifImageView.setImageURI(Uri.parse("file:///" + Environment.getExternalStorageDirectory() + "/updateFlag/b.gif"));
//gifImageView.setImageResource(R.drawable.b);

gifDrawable.start(); //Start playing
gifDrawable.stop(); //stop playing
gifDrawable.reset(); //Reset, restart playback
gifDrawable.isRunning(); //Is playing
gifDrawable.setLoopCount( 2 ); //Set the number of times to play, and stop automatically after playing
gifDrawable.getCurrentLoop(); //Get the number of times playing
gifDrawable.getCurrentPosition ; //Get the time from now to play
gifDrawable.getDuration() ; //Get the time needed to play once

Load network gif

The library does not support direct loading of network URLs, and needs to obtain streams or caches (to Baidu, Google)

Get the bitmap in the middle of network loading, cut the picture into circles, rotate, modify the animation, gray scale processing, size, etc

Posted by iron999mike on Wed, 01 Jan 2020 12:00:57 -0800