Android's multimedia Management Library Glide Basic use Example _android

Source: Internet
Author: User
Tags garbage collection

Glide is a fast and efficient Open-source multimedia resource Management library on the Android platform that provides compression of multimedia files, memory and disk caching, and resource pool interfaces.
Glide support acquisition, decompression display video, images and GIFs, Glide has a flexible API allows developers to customize the network stack technology, the default use of HttpURLConnection, you can be replaced by Google's volley or okhttp

Glide started with a quick and smooth display list of pictures, but Glide is useful for you to pull, resize, and display remote images.

The simplest use case for glide is to load pictures from a remote server or local file system, place them in the disk and memory cache, and load them onto view. It can be used in the city's picture app, and glide is optimized as smoothly as possible for scrolling lists that contain pictures.

Object Pool
the core of the glide principle is to maintain an object pool for bitmap. The primary purpose of object pooling is to improve performance by reducing the allocation of large objects for reuse.

Neither the Dalvik nor the art virtual machines use the compacting garbage collector,compacting garbage collector is a pattern in which the GC traverses the heap while moving the active object to the adjacent memory area. Allow larger chunks of memory to be used in subsequent allocations. Because Android does not have this pattern, it is possible that the assigned objects are scattered around and that only a small amount of memory is available between objects. If an application attempts to allocate an object that is larger than the contiguous block of free memory, it causes outofmemoryerror, and then crashes, even if the total spare memory space is larger than the object's size.

Using object pooling can also help improve scrolling performance, because reusing bitmap means fewer objects are created and recycled. Garbage collection causes the Stop all (Stop the World) event, which means that all threads (including the UI thread) are paused during the collector's execution. At this point, image frames cannot be rendered and the UI may stall, especially during scrolling.

Download
Glide GitHub Project Address Https://github.com/bumptech/glide

You can download the release page from the GitHub.

or use Gradle:

repositories {
 mavencentral ()
}

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

repositories {
 mavencentral ()
}
 
dependencies {
  compile ' com.github.bumptech.glide:glide:3.3.+ '
  compile ' com.android.support:support-v4:19.1.0 '
}

Maven

<dependency>
 <groupId>com.github.bumptech.glide</groupId>
 <artifactid>glide </artifactId>
 <version>3.3.1</version>
 <type>aar</type>
</ dependency>
<dependency>
 <groupId>com.google.android</groupId>
 <artifactid >support-v4</artifactId>
 <version>r7</version>
</dependency>

< dependency>
 <groupId>com.github.bumptech.glide</groupId>
 <artifactid>glide</ artifactid>
 <version>3.3.1</version>
 <type>aar</type>
</ dependency>
<dependency>
 <groupId>com.google.android</groupId>
 < artifactid>support-v4</artifactid>
 <version>r7</version>
</dependency>

Use

Glide is simple to use and automatically contains bitmap pooling without any special configuration.

Drawablerequestbuilder Requestbuilder = glide.with (context). Load (IMAGEURL);
Requestbuilder.into (ImageView);

That's all the requirements for loading a picture. Just as in many places in Andong, it is not clear what kind of context is in the with () method. It is important to remember that the incoming context type affects the degree to which glide loads the picture, glide can monitor the lifecycle of the activity and automatically cancel the wait request when the activity is destroyed. But if you use the application context, you lose this optimization effect.

Note: In fact, the above code is a comparison of the formulation of norms, we are more familiar with the writing is:

Glide.with
  . Load ("Http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
  . Into (IVIMG);

A simple example

For a simple view: @Override the public void OnCreate (Bundle savedinstancestate) {...

  ImageView ImageView = (imageview) Findviewbyid (R.id.my_image_view);
Glide.with (this). Load ("Http://goo.gl/h8qOq7"). into (ImageView); //For a list: @Override public View getview (int position, View recycled, ViewGroup container) {Final ImageView Myim
  Ageview;
  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). Crossfade ()

  . into (Myimageview);
return myimageview;
 
  //For a simple view: @Override the public void OnCreate (Bundle savedinstancestate) {...
 
  ImageView ImageView = (imageview) Findviewbyid (R.id.my_image_view);
Glide.with (this). Load ("Http://goo.gl/h8qOq7"). into (ImageView); }//For a 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). Crossfade ()
 
  . into (Myimageview);
return myimageview;

 }

Volley

If you want to use volley:

Gradle

dependencies {
  compile ' com.github.bumptech.glide:volley-integration:1.0.+ '
  compile ' Com.mcxiaoke.volley: library:1.0.+ '
}

dependencies {
  compile ' com.github.bumptech.glide:volley-integration:1.0.+ '
  Compile ' com.mcxiaoke.volley:library:1.0.+ '
}

Maven:

<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId> volley-integration</artifactid>
  <version>1.0.1</version>
  <type>jar</type >
</dependency>
<dependency>
  <groupId>com.mcxiaoke.volley</groupId>
  <artifactId>library</artifactId>
  <version>1.0.5</version>
  <type> aar</type>
</dependency>

<dependency>
  <groupId> com.github.bumptech.glide</groupid>
  <artifactId>volley-integration</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>
< dependency>
  <groupId>com.mcxiaoke.volley</groupId>
  <artifactid>library</ artifactid>
  <version>1.0.5</version>
  <type>aar</type>
</ Dependency>

And then in your activity or program, register volley as the basic module

public void OnCreate () {
 glide.get (this). Register (Glideurl.class, Inputstream.class,
    new Volleyurlloader.factory (Yourrequestqueue));
 ...
}

public void OnCreate () {
 glide.get (this). Register (Glideurl.class, Inputstream.class,
    new Volleyurlloader.factory (Yourrequestqueue));
 ...
}

Okhttp

Gradle:

dependencies {
  compile ' com.github.bumptech.glide:okhttp-integration:1.0.+ '
  compile ' com.squareup.okhttp:okhttp:2.0.+ '
}

dependencies {
  compile ' com.github.bumptech.glide:o khttp-integration:1.0.+ '
  compile ' com.squareup.okhttp:okhttp:2.0.+ '
}

Maven:

<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId> okhttp-integration</artifactid>
  <version>1.0.1</version>
  <type>jar</type >
</dependency>
<dependency>
  <groupId>com.squareup.okhttp</groupId>
  <artifactId>okhttp</artifactId>
  <version>2.0.0</version>
  <type>jar </type>
</dependency>

<dependency>
  <groupid>com.github.bumptech.glide </groupId>
  <artifactId>okhttp-integration</artifactId>
  <version>1.0.1</ version>
  <type>jar</type>
</dependency>
<dependency>
  < groupid>com.squareup.okhttp</groupid>
  <artifactId>okhttp</artifactId>
  < version>2.0.0</version>
  <type>jar</type>
</dependency>
 

And then in your activity or program, register volley as the basic module

public void OnCreate () {
 glide.get (this). Register (Glideurl.class, Inputstream.class,
    new Okhttpurlloader.factory (yourokhttpclient));
 ...
}

public void OnCreate () {
 glide.get (this). Register (Glideurl.class, Inputstream.class,
    new Okhttpurlloader.factory (yourokhttpclient));
 ...
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.