ตัวอย่างนี้สาธิตวิธีแปลงระหว่าง Bitmap และ Drawable ใน Android ทั้งสองทิศทาง
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout (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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Convert Bitmap to Drawable" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnConvert"
android:layout_marginTop="10dp" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button button;
Bitmap bitmap;
Drawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = findViewById(R.id.btnConvert);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 1. Drawable → Bitmap
drawable = getResources().getDrawable(R.drawable.image);
bitmap = ((BitmapDrawable) drawable).getBitmap();
// 2. Bitmap → Drawable (BitmapDrawable)
Drawable newDrawable = new BitmapDrawable(getResources(), bitmap);
// 3. แสดงผล
imageView.setImageDrawable(newDrawable);
}
});
}
}
สรุปการแปลงแบบต่างๆ
| การแปลง | โค้ด | หมายเหตุ |
|---|---|---|
| Drawable → Bitmap | ((BitmapDrawable) drawable).getBitmap() |
ต้องเป็น BitmapDrawable เท่านั้น |
| Bitmap → Drawable | new BitmapDrawable(resources, bitmap) |
ต้องส่ง Resources เข้าไป |
| VectorDrawable → Bitmap | ต้องใช้ Canvas วาดเอง | ไม่สามารถ Cast ได้โดยตรง |
| Drawable → Bitmap (ทุกชนิด) | ใช้ drawToBitmap() (API 29+) |
รองรับ VectorDrawable ด้วย |
โค้ดแปลง Drawable ทุกประเภทเป็น Bitmap (รองรับ Vector)
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// สำหรับ VectorDrawable และ Drawable อื่นๆ
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width <= 0) width = 1;
if (height <= 0) height = 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
การทดสอบ
รันแอป → กดปุ่ม "Convert Bitmap to Drawable" → ImageView จะแสดงรูปภาพจาก Drawable ที่แปลงมาจาก Bitmap
เคล็ดลับ
- ใช้
getResources().getDrawable(id, theme)สำหรับ API 21+ (รองรับ Theme) - จำเป็นต้อง
bitmap.recycle()เมื่อไม่ใช้แล้ว เพื่อหลีกเลี่ยง Memory Leak - สำหรับ Glide/Picasso: ใช้
.asBitmap()ได้ Bitmap โดยตรง