如何在Android Studio中添加Picasso库?


在学习Picasso库示例之前,我们应该了解Picasso。Picasso是一个图像处理库,由Square Inc.开发。过去,我们常常编写冗长的代码来从服务器获取图像或进行处理,为了优化这个过程,Picasso应运而生。

此示例演示如何在Android Studio中集成Picasso库。

步骤1 - 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。

步骤2 - 在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'
}

步骤3 - 将以下代码添加到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>

步骤4 - 将以下代码添加到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://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();
         }
      });
   }
}

在上面的代码中,我们有很多与Picasso相关的方法,如下所示:

  • with() - 我们必须为Picasso库传递上下文

  • load() - 我们要加载到Picasso中的内容,必须提供路径,无论是本地目录还是互联网资源

  • resize() - 如果你想调整图像大小,可以使用一定的宽度和高度。

  • centerCrop() - 你可以对你的ImageView进行中心裁剪。

  • rotate() - 你可以将图像旋转0到360度

  • into() - 你想在哪个视图中显示图像,我们必须提供ImageView路径,并且还有两个回调函数可用,如下所示:

  • onSuccess() - 如果图像成功下载,你可以执行任何操作。

  • onError() - 如果图像下载失败,你可以执行任何操作。

让我们尝试运行你的应用程序。我假设你已将你的实际Android移动设备连接到你的电脑。要在Android Studio中运行应用程序,打开项目中的一个Activity文件,然后点击运行 播放图标 工具栏中的图标。选择你的移动设备作为选项,然后检查你的移动设备,它将显示你的默认屏幕。

Logo Resize

现在你可以观察上面的图像,上面的图像根据我们在resize()中给定的尺寸进行了裁剪。现在我们去掉了centerCrop()和resize方法,它将显示默认大小的图像,如下所示。

TP Logo

点击 这里 下载项目代码

更新于:2020年1月27日

浏览量:1000+

启动你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.