In this tutorial, we will know that how to implement Picasso Library to loading images in android.
This is an open source library to loading images in Android. It provides a functionality to fast processing and caching data into memory. It developed by Square. Android uses many others libraries to loading images but Picasso is one of the easiest ways to manage.
Benefit of Picasso Library:-
- Picasso loading image from URL
- It maintains the cache of images and avoids the overhead of writing code manually.
- It uses minimal memory for complex images. It avoids the OutOfMemoryException.
Step1- Adding Picasso Library in the project.
We need to add following library in project build.gradle file as a dependency.
dependencies {
compile 'com.squareup.picasso:picasso:2.5.2'
}
Step2- Adding ImageView in your activity_main.xml file
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Step3- Initialize ImageView in Activity onCreate() method
Initialize imageview id within onCreate() method to load image from URL.
ImageView imageView = (ImageView) findViewById(R.id.imageview);
Step4- Download the image from URL.
Next, we need to download the images from URL and set to ImageView view. We pass context, URL, and ImageView to Picasso Object.
Use the following code given below:
Picasso.with(this) // "this" is the context of activity
.load("http://i.imgur.com/DvpvklR.png") // here is your image url.
.into(imageView); // your image view
0 Comment(s)