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

วิธีใช้ Glide โหลดรูปภาพเป็น Bitmap ใน Android

การใช้งาน Glide โหลดรูปภาพเป็น Bitmap ใน Android

ตัวอย่างนี้สาธิตการใช้ Glide ของ BumpTech เพื่อดาวน์โหลดรูปภาพจาก URL และแปลงเป็น Bitmap โดยตรง

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

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

implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'

หมายเหตุ: ใช้เวอร์ชันล่าสุดแทน 4.9.0 และเพิ่ม annotationProcessor สำหรับ GeneratedAPI

ขั้นตอนที่ 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"
    tools:context=".MainActivity">
    <ImageView android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

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

        Glide.with(this)
            .asBitmap()
            .load("https://www.google.es/images/srpr/logo11w.png")
            .into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    imageView.setImageBitmap(resource);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {
                    // ทำความสะอาดเมื่อโหลดใหม่หรือยกเลิก
                }
            });
    }
}

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

💡 เปรียบเทียบ Glide vs Picasso

คุณสมบัติGlidePicasso
รองรับ GIF/Video✅ ใช้ได้ดี❌ ไม่รองรับ
Memory Managementดีกว่า (Bitmap Pool)ดี
ขนาดไลบรารีใหญ่กว่า (~400KB)เล็กกว่า (~100KB)
SyntaxFluent APIFluent API
แปลงเป็น Bitmap.asBitmap().get() (blocking)

การใช้งาน Glide แบบง่าย (ไม่ต้อง Bitmap)

Glide.with(this)
    .load(imageUrl)
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .into(imageView);

การทดสอบ

รันแอป จะโหลดโลโก้ Google แสดงใน ImageView โดยอัตโนมัติ