Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में Bitmap को Drawable में कैसे कन्वर्ट करें?

एंड्रॉइड में Bitmap और Drawable के बीच कन्वर्जन

यह उदाहरण दिखाता है कि `BitmapDrawable` क्लास का उपयोग करके Bitmap को Drawable में और Drawable को Bitmap में कैसे बदला जाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML

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="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"
        android:scaleType="centerInside"
        android:contentDescription="परिणाम इमेज" />

</RelativeLayout>

चरण 3: MainActivity में कन्वर्जन लॉजिक

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 {
    private ImageView imageView;
    private Button btnConvert;

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

        imageView = findViewById(R.id.imageView);
        btnConvert = findViewById(R.id.btnConvert);

        btnConvert.setOnClickListener(v -> convertBitmapToDrawable());
    }

    private void convertBitmapToDrawable() {
        // 1. ड्रॉएबल संसाधन से ड्रॉएबल प्राप्त करें
        Drawable drawable = getResources().getDrawable(R.drawable.your_image, getTheme());

        // 2. Drawable → Bitmap (यदि BitmapDrawable है)
        Bitmap bitmap = null;
        if (drawable instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else {
            // वेक्टर ड्रॉएबल आदि के लिए बिटमैप बनाएं
            bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        }

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

        // 4. ImageView में सेट करें
        imageView.setImageDrawable(newDrawable);
    }
}

🔄 कन्वर्जन सारांश

दिशाकोडनोट
Drawable → Bitmap`((BitmapDrawable) drawable).getBitmap()`केवल BitmapDrawable के लिए काम करता है
Drawable → Bitmap (सामान्य)`Bitmap.createBitmap()` + `Canvas`किसी भी Drawable के लिए काम करता है
Bitmap → Drawable`new BitmapDrawable(resources, bitmap)`सबसे सामान्य तरीका
Bitmap → Drawable (API 22+)`BitmapDrawable.create(resources, bitmap)`फैक्टरी मेथड

⚠️ महत्वपूर्ण नोट्स

  • म्यूटेबल बिटमैप: यदि बिटमैप को संशोधित करना है तो `bitmap.copy(Bitmap.Config.ARGB_8888, true)` उपयोग करें
  • मेमोरी: बड़े बिटमैप को रिसाइकल करें: `bitmap.recycle()`
  • थीम: `getDrawable(id, theme)` API 21+ के लिए उपयोग करें
  • वेक्टर ड्रॉएबल: सीधे बिटमैप में कास्ट नहीं किया जा सकता, Canvas पर ड्रॉ करना पड़ता है

चरण 4: Manifest

<?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" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। "Bitmap को Drawable में बदलें" बटन दबाने पर इमेज ImageView में दिखाई देगी।

  1. Android में मिलीसेकंड को डेट फॉर्मेट में कैसे बदलें? Android में मिलीसेकंड को डेट फॉर्मेट में कैसे बदलें?

    Android में मिलीसेकंड से डेट कन्वर्जन गाइड यह ट्यूटोरियल दिखाता है कि कैसे epoch मिलीसेकंड को पढ़ने योग्य डेट फॉर्मेट में बदला जाता है, SimpleDateFormat और आधुनिक java.time API का उपयोग करके। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट फाइल res/layout/activity_main.

  1. Android में Drawable के रंग कैसे बदलें? Android में Drawable के रंग कैसे बदलें?

    Android में ड्रॉएबल के रंग प्रोग्रामेटिकली बदलना यह ट्यूटोरियल दिखाता है कि कैसे AnimationDrawable और GradientDrawable का उपयोग करके ड्रॉएबल के रंग डायनामिकली बदले जाते हैं, जिससे रनटाइम पर थीम बदलना या विजुअल इफेक्ट्स बनाना संभव होता है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाए

  1. Android पर स्क्रीनशॉट को PDF में कैसे बदलें Android पर स्क्रीनशॉट को PDF में कैसे बदलें

    महत्वपूर्ण जानकारी के स्क्रीनशॉट लेना बहुत उपयोगी है, खासकर अगर कोई विशेष PDF फ़ाइल उपलब्ध नहीं है जिसे बस रखा जा सके। PDF महत्वपूर्ण जानकारी को उनके मूल रूप में संग्रहीत करने के लिए एक सामान्य प्रारूप है, छवियों जैसी अन्य फ़ाइलों के विपरीत जिनका प्रारूप लगातार बदलता रहता है जहां उन्हें साझा किया जा