In Android, there may be a need to get pictures from the Internet, so this feature is best with third-party frameworks, because the third-party framework encapsulates a lot of operations for us, such as the glide framework, which can load images asynchronously, and can set up a series of functions such as caching and thumbnails. Below is the introduction of how to use.
First add Gradle:
Compile ' com.github.bumptech.glide:glide:3.7.0 '
Add the appropriate permissions to access the network and access local storage:
<uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>
The simplest way to use it:
Glide . With (this) . Load ("Http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png") . into (ImageView);
Introduction of Glide with:
- With (context context). With the application context, glide requests are not controlled by the activity/fragment lifecycle.
- With (activity activity). Using activity as a context, glide requests are subject to activity lifecycle control.
- With (fragmentactivity activity). Glide requests are subject to fragmentactivity lifecycle control.
- With (Android.app.Fragment Fragment). Glide requests are subject to fragment lifecycle control.
- With (Android.support.v4.app.Fragment Fragment). Glide requests are subject to fragment lifecycle control.
Set up bitmaps and animations
When the picture is too large or the internet is too slow, you can set a bitmap for the image, and display the bitmap settings as the bitmap before the image is loaded
You can also set the image to display if the image fails to load
Glide.with (this). Load ("Http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") . Placeholder ( R.drawable.ic_launcher)//Set up the bitmap to display before loading . Error (R.drawable.icon)//display when image loading fails . into (image);
When you load an image, you can also animate the image display:
Glide.with (this). Load ("Http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") . Placeholder ( R.drawable.ic_launcher) . Error (R.drawable.icon) . Crossfade ()//Set display animation, . into (image);
crossFade()There are several methods of overloading
crossFade(int duration): Set the time
crossFade(Animation animation, int duration): Set custom animations and times
crossFade(int animationId, int duration): Loading animation resources and timethumbnail ImagesGlide can also set thumbnails, that is, to load thumbnails before loading the original image:
Glide. With (context). Load (usageexamplegifandvideos.gifurl) . Thumbnail (0.1f) . into ( IMAGEVIEW2);
Cache
We all know that image processing and display in Android is the most memory-intensive, easy to cause oom problem, generally in the image processing and display, will use the cache policy, memory cache or hard disk cache, Glide also provides a different cache policy, By default, the displayed image is cached in memory, or you can set the call method without using the memory cache, skipMemoryCache(true) telling Glide us that we do not intend to use the memory cache, the default is to use the memory cache
Glide.with (this) . Load ("Http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") . Skipmemorycache (True) . into (image);
GlideAlso provides a hard disk cache, hard disk cache policy can be set by the method diskCacheStrategy()
Glide.with (this) . Load ("Http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") . Diskcachestrategy (Diskcachestrategy.source)//Do not use hard disk cache . into (image);
Hard disk cache policy
DiskCacheStrategy.NONE: Do not use hard disk cache
DiskCacheStrategy.SOURCE: Caches the original image on the hard disk
DiskCacheStrategy.RESULT: Caches the displayed size of the image on the hard disk (default cache policy)
DiskCacheStrategy.ALL: Both the displayed image and the original image are cached
Android Glide Frame Use