Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ไลบรารี Picasso โหลดรูปภาพใน Android

การใช้งานไลบรารี Picasso สำหรับโหลดและแสดงรูปภาพใน Android

ตัวอย่างนี้สาธิตการใช้ Picasso ของ Square Inc. เพื่อดาวน์โหลดและแสดงรูปภาพจาก URL ลงใน ImageView อย่างง่ายและมีประสิทธิภาพ

ขั้นตอนที่ 1: สร้างโปรเจกต์และเพิ่ม Dependency

สร้างโปรเจกต์ใหม่ แล้วเพิ่มใน build.gradle (Module: app):

implementation 'com.squareup.picasso:picasso:2.71828'

หมายเหตุ: ใช้เวอร์ชันล่าสุดแทน 2.4.0 ที่ล้าสมัย

ขั้นตอนที่ 2: 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    tools:context=".MainActivity">
    <ImageView android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btnDownload"/>
    <Button android:id="@+id/btnDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"
        android:text="Download Image"/>
</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (MainActivity.java)

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    Button btnDownload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        btnDownload = findViewById(R.id.btnDownload);

        btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Picasso.get()  // ใช้ get() แทน with(context) ในเวอร์ชันใหม่
                    .load("https://images.pexels.com/photos/1212487/pexels-photo-1212487.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                    .placeholder(R.drawable.placeholder)  // รูประหว่างโหลด (optional)
                    .error(R.drawable.error_image)        // รูปเมื่อโหลดล้มเหลว (optional)
                    .into(imageView);
            }
        });
    }
}

ขั้นตอนที่ 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.INTERNET"/>
    <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>

💡 ฟีเจอร์เด่นของ Picasso

  • Caching อัตโนมัติ - จัดการหน่วยความจำและดิสก์แคชให้เอง
  • Placeholder & Error - แสดงรูปสำรองระหว่างโหลด/ล้มเหลว
  • Resize & Transform - .resize(w, h), .centerCrop(), .circleCrop()
  • Priority - กำหนดลำดับความสำคัญการโหลด
  • Callback - รับเหตุการณ์ success/error

ตัวอย่างการใช้งานขั้นสูง

Picasso.get()
    .load(imageUrl)
    .resize(300, 300)
    .centerCrop()
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .into(imageView, new Callback() {
        @Override public void onSuccess() { /* สำเร็จ */ }
        @Override public void onError(Exception e) { /* ล้มเหลว */ }
    });

การทดสอบ

รันแอป กดปุ่ม "Download Image" จะโหลดรูปจาก Pexels แสดงใน ImageView