How to add picasso library in android studio?

AndroidMobile DevelopmentProgramming

Before getting into picasso library example, we should know about picasso. Picasso is image processing library and developed by Square Inc. In older days we used to write lengthy of codes to grab image from server or do process., to optimize the process picasso introduced.

This example demonstrate about how to integrate picasso library in android studio.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code in build.gradle.

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   implementation 'com.squareup.picasso3:picasso:2.71828'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Step 3 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
<LinearLayout
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:gravity = "center"
   android:orientation = "vertical">
   <ImageView
      android:id = "@+id/imageView"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Step 4 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ImageView imageView=findViewById(R.id.imageView);
      Picasso.with(this)
      .load("https://www.tutorialspoint.com/images/tp-logo-diamond.png")
      .placeholder(R.mipmap.ic_launcher)
      .resize(400, 400)
      .centerCrop()
      .rotate(0)
      .into(imageView, new Callback() {
         @Override
         public void onSuccess() {
            Toast.makeText(getApplicationContext(), "Fetched image from internet", Toast.LENGTH_SHORT).show();
         }
         @Override
         public void onError() {
            Toast.makeText(getApplicationContext(), "An error occurred", Toast.LENGTH_SHORT).show();
         }
      });
   }
}

In the above code we have so many methods are associate with picasso as shown below.

  • with() − we have to pass context for the piasso library

  • load() − what we want to load in picass we have to give that path either it is local directory or internet source

  • resize() − if you want to resize your image, you can do that with certain width and height.

  • centercrop() − you can do center crop to your image view.

  • rotate() − you can rotate your image as 0 to 360 degreees

  • into() − In which view you want to show, we have to give imageview paths and there are two call backs are available as shown below

  • onSuccess() − if image is successfully downloaded, you can do any action.

  • onError() − if image is not successfully downloaded, you can do any action.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen.

Logo Resize

Now you can observe the above image, The above image is cropped according to size which we have given in resize(). now we have removed the centerCrop() and resize method, it will show image with default size as shown below.

TP Logo

Click here to download the project code

raja
Published on 05-Dec-2018 10:53:45
Advertisements