Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

Android 스튜디오에 피카소 라이브러리를 추가하는 방법은 무엇입니까?

<시간/>

피카소 라이브러리 예제에 들어가기 전에 피카소에 대해 알아야 합니다. Picasso는 Square Inc.에서 개발한 이미지 처리 라이브러리입니다. 예전에는 피카소가 도입된 프로세스를 최적화하기 위해 서버에서 이미지를 가져오거나 프로세스를 수행하기 위해 긴 코드를 작성하곤 했습니다.

이 예제는 안드로이드 스튜디오에서 피카소 라이브러리를 통합하는 방법을 보여줍니다.

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 = "https://schemas.android.com/apk/res/android" xmlns:tools = "https://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://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();
         }
      });
   }
}

위의 코드에는 아래와 같이 피카소와 관련된 많은 메서드가 있습니다.

  • 함께() − piasso 라이브러리에 대한 컨텍스트를 전달해야 합니다.

  • 로드() − picass에 로드하려는 항목은 해당 경로에 로컬 디렉토리 또는 인터넷 소스를 지정해야 합니다.

  • 크기 조정() − 이미지 크기를 조정하려면 특정 너비와 높이로 크기를 조정할 수 있습니다.

  • 중앙 자르기() - 이미지 보기에 중앙 자르기를 할 수 있습니다.

  • 회전() − 이미지를 0~360도 회전할 수 있습니다.

  • 안으로() − 어떤 보기를 표시하고 싶은지 이미지 보기 경로를 지정해야 하며 아래와 같이 두 개의 콜백을 사용할 수 있습니다.

  • onSuccess() − 이미지가 성공적으로 다운로드되면 모든 작업을 수행할 수 있습니다.

  • onError() − 이미지가 성공적으로 다운로드되지 않으면 아무 조치를 취하셔도 됩니다.

응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행을 클릭하세요. 툴바에서 Android 스튜디오에 피카소 라이브러리를 추가하는 방법은 무엇입니까? 아이콘. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오.

Android 스튜디오에 피카소 라이브러리를 추가하는 방법은 무엇입니까?

이제 위의 이미지를 볼 수 있습니다. 위의 이미지는 우리가 resize()에서 지정한 크기에 따라 잘립니다. 이제 centerCrop() 및 크기 조정 메서드를 제거하면 아래와 같이 기본 크기의 이미지가 표시됩니다.

Android 스튜디오에 피카소 라이브러리를 추가하는 방법은 무엇입니까?