Detailed Basic Use of Android Glide Picture Loading Library

Keywords: Android Fragment Google Gradle

Preface

Picture loading is indispensable in Android development projects. In order to reduce the development cycle and difficulty, we often choose some open source image loading libraries. Now, Android has developed into more and more open source image loading libraries. Here we introduce Glide open source image loading libraries.

brief introduction

Glide is an open source image loading Library of Google. It is a fast and efficient Android open source media management and image loading framework. It packages media decoding, memory and disk caching, and resource pool into a simple and easy-to-use interface.

Functional introduction and basic use

1. configuration

  • Adding dependencies to Project gradle
repositories {
  mavenCentral()
  google()
}
  • Adding dependencies to Module's gradle
dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
  • Adding network privileges
<uses-permission android:name="android.permission.INTERNET"/>
  • Basic use
ImageView mImageView = (ImageView) findViewById(R.id.ImageView);
        String Url = "http://***********";

        Glide .with(this)
                .load(Url)
                .into(targetImageView);

2. Introduction to Basic Functions - Use

  • Asynchronous loading of pictures (basic functions)
ImageView mImageView = (ImageView) findViewById(R.id.ImageView);
        String Url = "http://**********";

//Glide uses the call mode of streaming interface
        Glide.with(context).load(Url).into(targetImageView);
  • Setting Picture Loading Size
Glide.with(this).load(imageUrl).override(500, 500).into(imageView);
  • Set up loading and loading failure pictures
Glide
 .with(this)
  .load(imageUrl)
 .placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
  • Setting up loading animation
Glide.with(this).load(imageUrl).animate(R.anim.item_alpha_in).into(imageView);
  • Set the content to load (image-text mix)
Glide.with(this).load(imageUrl).centerCrop().into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                imageView.setImageDrawable(resource);
            }
        });
  • Diversified media loading
Glide
        .with(context)
        .load(imageUrl);
        .thumbnail(0.1f);//Setting up thumbnail support: Load the thumbnail (10% of the original image) first and then load the full image.
//Prime.
        .asBitmap()//Display gif static pictures 
        .asGif();//Display gif dynamic pictures
        .into(imageView);
  • Setting up disk caching policy
Glide.with(this).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);

// Cache parameter description
// DiskCacheStrategy.NONE: Disable disk caching without caching any pictures
// DiskCacheStrategy.ALL: Cache the original image - the converted image (default)
// DiskCacheStrategy.SOURCE: Cache only the original image (the original full resolution image, i.e. the converted image is not cached)
// DiskCacheStrategy.RESULT: Only converted pictures are cached (i.e. final images: after resolution reduction or conversion, original pictures are not cached).
  • Clear cache
Glide.get(this).clearDiskCache();//Cleaning disk caches requires execution in sub-threads 
Glide.get(this).clearMemory();//Cleaning up memory caches can be done in the UI main thread
  • Life cycle integration
 Glide.with(Context context)// Binding Context
        .with(Activity activity);// Binding Activity
        .with(FragmentActivity activity);// Binding FragmentActivity
        .with(Fragment fragment);// Binding Fragment

So far, the basic use of Glide image loading library has been explained. Thank you for reading.

Welcome to Pay Attention to Authors darryrzhong More dry goods are waiting for you.

Please reward a little red heart! Because your encouragement is my greatest motivation to write!

More wonderful articles please pay attention to

Posted by erax on Wed, 15 May 2019 04:10:29 -0700