Glide — Getting Started
After receiving so much successful feedback on our Picasso series, we are going to start another amazing line of image loading libraries: Glide.
Glide, like Picasso, can load and display images from multiple sources while also balancing caching and maintaining a low memory consumption while doing image processing. It has been used on official Google apps (such as the Google Developer Conference 2015 APP) and is as popular as Picasso. In this series, we explore the differences and advantages of Glide and Picasso.
Glide Series Preview
- To start!
- Loaded into the order
- ListAdapter(ListView, GridView)
- Placeholders and fade animations
- Images are resized and scaled
- Display Gif and Video
- Cache based
- Request priority
- The thumbnail
- Callbacks: SimpleTarget and ViewTarget are used for custom view classes
- Load images into notifications and application widgets
- Exceptions: debugging and error handling
- Custom conversion
- Customize animation with animate()
- Integrated network stack
- Custom Glide with Module
- Module instance: accepts HTTPS for self-signed certificates
- Module instance: Custom cache
- Module instance: Optimized loaded images with custom sizes
- Use the Model Loader dynamically
- How to rotate an image
- Series of reviews
Why use Glide?
Experienced Android developers can skip this section, but for starters, you may be wondering why you want to use Glide instead of implementing it yourself.
Android is a bit awkward when it comes to handling images, as it loads images into memory in pixels. The average phone camera size for a photo is 2592×193 pixels (5 megapixels) and will allocate about 19MB of memory. For complex network situations, caching, and image processing, you can save a lot of time and headaches if you use a tested and developed library such as Glide!
In this series, we’ll look at many of Glide’s features. Take a look at the outline of this blog post and consider whether you really want to develop all of these features.
Add the Glide
Hopefully we’ve now convinced you to use a library to handle your image loading requests. If you want to know more about Glide, this is the guide for you!
First, add Glide to your dependencies. As this blog is written, the latest version is 3.6.1.
Gradle
As with most dependencies, in a Gradle project add the following line of code to your build. Gradle:
The compile 'com. Making. Bumptech. Glide: glide: 3.6.1'Copy the code
Maven
Glide also supports Maven projects:
Com. Making. Bumptech. Glide glide 3.6.1 aarCopy the code
First time: Load an image from a URL
Like Picasso, Glide Library uses the Fluent Interface. Glide builder requires a minimum of three parameters for a complete functional request.
with(Context context)
– For many Android API calls,ContextIt’s a must. Glide is the same hereload(String imageUrl)
– Here you can specify which image should be loaded, as above it will be a string representing the URL of a web imageinto(ImageView targetImageView)
Your image will be displayed in the corresponding ImageView.
Theoretical explanations tend to fall short, so take a look at practical examples:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
String internetUrl = "http://i.imgur.com/DvpvklR.png";
Glide
.with(context)
.load(internetUrl)
.into(targetImageView);
Copy the code
In this way! If the URL for the image exists and your ImageView is visible, you will see the image in a few seconds. Glide returns an error callback in case the image does not exist (we will discuss this later). You may already believe that these three lines of code are useful to you, but they’re just the tip of the iceberg.
Looking forward to
In the next article, we’ll start loading images with options other than loading from a web URL. Specifically, we’ll load images from Android resources, local files, and a Uri.