Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách chuyển đổi Bitmap sang Drawable trong Android

Hướng dẫn này minh họa cách chuyển đổi hai chiều giữa Bitmap và Drawable bằng BitmapDrawable.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Giao diện (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="Chuyển Bitmap → 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>

Bước 3: Logic chuyển đổi (src/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;

    @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(v -> {
            // 1. Lấy Drawable từ resource
            Drawable drawable = getResources().getDrawable(R.drawable.image, getTheme());

            // 2. Drawable → Bitmap
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            // 3. Bitmap → Drawable mới
            Drawable newDrawable = new BitmapDrawable(getResources(), bitmap);

            // 4. Hiển thị
            imageView.setImageDrawable(newDrawable);
        });
    }
}

🔄 Tóm tắt chuyển đổi

Chuyển đổiCode
Drawable → Bitmap((BitmapDrawable) drawable).getBitmap()
Bitmap → Drawablenew BitmapDrawable(resources, bitmap)
Resource → BitmapBitmapFactory.decodeResource(res, R.drawable.xxx)
Resource → DrawableContextCompat.getDrawable(context, R.drawable.xxx)
Drawable → Bitmap (Vector)Cần render lên Canvas (VectorDrawable không ép được trực tiếp)

Bước 4: Manifest (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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nhấn nút để chuyển đổi và hiển thị hình ảnh.