Picasso Android Tutorial for Beginners – Android Picasso Example

Introduction to Picasso Android Library

In many of your Android applications when you want to load bulky images, it consumes a lot of memory. As the memory load the images pixel by pixel. In this scenario, you need image caching. Picasso Android library has figured out this problem, by providing for downloading and caching images with less effort. You may also have an eye on other libraries which provide image caching features. But with the help of Picasso library, you can do it with a few lines of code and with better performance. Picasso deals with the memory and takes care of all the caching processes.

Setting Up Picasso Library

Before using Picasso Android library, you must add Picasso dependency for your Android application. For this purpose open build.gradle file and in the dependencies section and add “com.squareup.picasso:picasso:2.5.2”:

Picasso also supports Maven projects. You can configure Maven dependencies as:

After you set up the dependencies, you must Sync your project.

The application using Picassa Android Library also needs internet permission. To add internet permissions in Android Manifest file

Loading Images into ImageView

First of all, create an ImageView with the help of drag and drop or you can write XML yourself:

 

When using Picassa, the simple basic example to understand Picassa Android library is the loading of an image from a URL. You will need to specify the source and the destination of the image. The source is the URL from where you want to load the image from and the destination is the ImageView for placing the image.

Picasso Android Library needs three basic parameters when loading an image from a URL. i.e. Context, URL and ImageView.

From URL

If you want to load the image from a URL, then you must place the URL inside the load() method.

From Drawable Resource

If you want to load the image from drawable resources inside your application, then you must place the reference of the resource file instead of the URL inside the load() method.

Image in Android Drawable Resources
Image in Android Drawable Resources

So far we can load the image into an ImageView. With two type of Resources. First is from a URL to ImageView and second is from local drawable resource file to ImageView.

Rotating Image

Moving further, let’s see how to rotate the image in ImageView. With Picasso Android library you can specify the degrees to rotate the image. There are two methods to rotate.

  1. Basic Rotation
        rotate(float degrees)

    Picasso Simple Rotate 90 degree
    Picasso Rotate 90 degree

  2. Advanced Rotation
        rotate(float degrees, float pivotX, float pivotY)

    Picasso Complex Rotation
    Picasso Complex Rotation


     

Resizing Image

When using Picasso Android library you can resize the image to specific height and width. resize(horizontalSize, verticalSize) method set the size of the image to specific size.

Other Parameters

placeholder()

placeholder(<drawable resource>) display an image from the drawable resources until the resource from the internet is not loaded. As it takes some time to load the image into ImageView from the URL.

error()

If the application fails to load the image from the URL error(<local resource>) displays the error image. It acts as error callback. Placeholder displays a local image while the image from URL is loading. The error() is a bit similar to the placeholder().

onlyScaleDown()

When the image has the size larger than the target size of resize() attribute onlyScaleDown() method allows the resizing otherwise the size of the image will remain the same. It is used with resize().

centerCrop()

centerCrop() is also used with resize(). It is an advanced cropping method. centerCrop() crops the image to fill the ImageView but it doesn’t display the complete image. The visible image depends on the size of the actual image.

centerInside()

The centerInside() is also a cropping method but it works a bit different. With this cropping technique, the complete image is displayed but it may not fill the ImageView. It scales the image so that both dimensions are equal to or less than the requested bounds of ImageView

noFade()

 Picasso automatically softens the image in ImageView by fading it. If you don’t need the fading effect and need the actual image then you can use noFade().

fit()

The fit() attribute of Picasso Android library reduces the size of the image according to the ImageView internally.

You can also use Picasso Android library to populate ListView and GridView and RecyclerView

Download Android Picasso Example

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay blessed.