কম্পিউটার টিউটোরিয়াল

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে Drawable-কে Bitmap-এ কনভার্ট কীভাবে করবেন?

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে Drawable-কে Bitmap-এ কনভার্ট করা যায়।

পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।

পদক্ষেপ ২: res/layout/activity_main.xml-এ কোড যোগ করুন

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:gravity="center" 
    android:orientation="vertical" 
    android:padding="4dp" 
    tools:context=".MainActivity"> 
    <ImageView 
        android:id="@+id/imageView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
    <Button 
        android:id="@+id/btnConvert" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Convert Drawable to Bitmap" /> 
</LinearLayout>

পদক্ষেপ ৩: src/MainActivity.kt-এ কোড যোগ করুন

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val imageView = findViewById<ImageView>(R.id.imageView)
        val btnConvert = findViewById<Button>(R.id.btnConvert)

        btnConvert.setOnClickListener {
            // পদ্ধতি ১: BitmapFactory ব্যবহার করে (রিসোর্স থেকে)
            val bitmap1 = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_foreground)
            imageView.setImageBitmap(bitmap1)
            
            // পদ্ধতি ২: Drawable-কে Bitmap-এ কনভার্ট (এক্সটেনশন ফাংশন)
            val drawable: Drawable = resources.getDrawable(R.drawable.ic_launcher_foreground, theme)
            val bitmap2 = drawable.toBitmap()
            
            Toast.makeText(
                applicationContext, 
                "Drawable converted to Bitmap: ${bitmap2.width}x${bitmap2.height}", 
                Toast.LENGTH_SHORT
            ).show()
        }
    }
}

private fun Drawable.toBitmap(): Bitmap {
    return if (this is BitmapDrawable) {
        this.bitmap
    } else {
        val bitmap = Bitmap.createBitmap(
            intrinsicWidth, 
            intrinsicHeight, 
            Bitmap.Config.ARGB_8888
        )
        val canvas = android.graphics.Canvas(bitmap)
        setBounds(0, 0, canvas.width, canvas.height)
        draw(canvas)
        bitmap
    }
}
বিভিন্ন পদ্ধতি:
  • BitmapFactory.decodeResource() - রিসোর্স আইডি থেকে সরাসরি
  • Drawable.toBitmap() - এক্সটেনশন ফাংশন (AndroidX)
  • Canvas ব্যবহার করে কাস্টম ড্রয়েবল রেন্ডার

পদক্ষেপ ৪: androidManifest.xml-এ কোড যোগ করুন

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.q11"> 
    <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>

অ্যাপ্লিকেশন চালান

আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. Picasso লাইব্রেরি ব্যবহার করে অ্যান্ড্রয়েডে URL থেকে ImageView-এ ছবি লোড করার পদ্ধতি

  2. অ্যান্ড্রয়েডে কোড ব্যবহার করে ImageView-এর মার্জিন সেট করার পদ্ধতি

  3. অ্যান্ড্রয়েডে Bitmap কে Drawable-এ রূপান্তর করা

  4. অ্যান্ড্রয়েডে Drawable কে Bitmap-এ রূপান্তর করা