이 예는 Picasso로 작업하는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity"> <Button android:id="@+id/btnDrawable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:text="Drawable" /> <ImageView android:id="@+id/imageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerHorizontal="true" android:src="@mipmap/ic_launcher" /> <Button android:id="@+id/btnPlaceholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnUrl" android:layout_alignBottom="@+id/btnUrl" android:layout_centerHorizontal="true" android:text="Placeholder" /> <Button android:id="@+id/btnUrl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnDrawable" android:layout_alignBottom="@+id/btnDrawable" android:layout_alignParentEnd="true" android:text="URL" /> <Button android:id="@+id/btnError" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnDrawable" android:layout_alignParentStart="true" android:layout_marginTop="12dp" android:text="Error" /> <Button android:id="@+id/btnCallBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnError" android:layout_alignStart="@+id/btnPlaceholder" android:layout_alignBottom="@+id/btnError" android:text="Callback" /> <Button android:id="@+id/btnResize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnCallBack" android:layout_alignBottom="@+id/btnCallBack" android:layout_alignParentEnd="true" android:text="Resize" /> <Button android:id="@+id/btnRotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnError" android:layout_alignParentStart="true" android:layout_marginTop="12dp" android:text="Rotate" /> <Button android:id="@+id/btnScale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/btnCallBack" android:layout_alignBottom="@+id/btnTarget" android:text="Scale" /> <Button android:id="@+id/btnTarget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnRotate" android:layout_alignBottom="@+id/btnRotate" android:layout_alignParentEnd="true" android:text="Targets" /> </RelativeLayout>
3단계 − src/MainActivity.java
에 다음 코드 추가package app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import com.squareup.picasso.Target; public class MainActivity extends AppCompatActivity implements View.OnClickListener { ImageView imageView; int i = 0; Button btnDrawableImage, btnUrlImage, btnErrorImage, btnPlaceholderImage, btnCallback, btnResizeImage, btnRotateImage, btnScaleImage, btnTarget; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { imageView = findViewById(R.id.imageView); btnDrawableImage = findViewById(R.id.btnDrawable); btnUrlImage = findViewById(R.id.btnUrl); btnPlaceholderImage = findViewById(R.id.btnPlaceholder); btnErrorImage = findViewById(R.id.btnError); btnCallback = findViewById(R.id.btnCallBack); btnResizeImage = findViewById(R.id.btnResize); btnRotateImage = findViewById(R.id.btnRotate); btnScaleImage = findViewById(R.id.btnScale); btnTarget = findViewById(R.id.btnTarget); btnDrawableImage.setOnClickListener(this); btnPlaceholderImage.setOnClickListener(this); btnUrlImage.setOnClickListener(this); btnCallback.setOnClickListener(this); btnResizeImage.setOnClickListener(this); btnErrorImage.setOnClickListener(this); btnRotateImage.setOnClickListener(this); btnScaleImage.setOnClickListener(this); btnTarget.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnDrawable: Picasso.with(this).load(R.drawable.image).into(imageView); break; case R.id.btnPlaceholder: Picasso.with(this).load("www.journaldev.com").placeholder(R.drawable.placeholder).into(imageView); break; case R.id.btnUrl: Picasso.with(this).load("https://cdn.journaldev.com/wp-content/uploads/2017/01/android- constraintlayout-sdk-tool-install.png").placeholder(R.drawable.placeholder).into(imageView); break; case R.id.btnError: Picasso.with(this).load("www.journaldev.com").placeholder(R.drawable.placeholder).error(R.drawable. error).into(imageView); break; case R.id.btnCallBack: Picasso.with(this).load("www.journaldev.com").error(R.mipmap.ic_launcher).into(imageView, new Callb ack() { @Override public void onSuccess() { Log.d("TAG", "onSuccess"); } @Override public void onError() { Toast.makeText(getApplicationContext(), "An error occurred", Toast.LENGTH_SHORT).show(); } }); break; case R.id.btnResize: Picasso.with(this).load(R.drawable.image).resize(200, 200).into(imageView); break; case R.id.btnRotate: Picasso.with(this).load(R.drawable.image).rotate(90f).into(imageView); break; case R.id.btnScale: if (i == 3) i = 0; else { if (i == 0) { Picasso.with(this).load(R.drawable.image).fit().into(imageView); Toast.makeText(getApplicationContext(), "Fit", Toast.LENGTH_SHORT).show(); } else if (i == 1) { Picasso.with(this).load(R.drawable.image).resize(200, 200).centerCrop().into(imageView); Toast.makeText(getApplicationContext(), "Center Crop", Toast.LENGTH_SHORT).show(); } else if (i == 2) { Picasso.with(this).load(R.drawable.image).resize(200, 200).centerInside().into(imageView); Toast.makeText(getApplicationContext(), "Center Inside", Toast.LENGTH_SHORT).show(); } i++; } break; case R.id.btnTarget: Picasso.with(this).load("https://cdn.journaldev.com/wp-content/uploads/2017/01/android- constraintlayout-sdk-tool- install.png").placeholder(R.drawable.placeholder).error(R.drawable.error).into(target); break; } } private Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { imageView.setImageBitmap(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { imageView.setImageDrawable(errorDrawable); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { imageView.setImageDrawable(placeHolderDrawable); } }; }
4단계 − androidManifest.xml에 다음 코드 추가
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행을 클릭합니다. 툴바에서 아이콘. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -