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

অ্যান্ড্রয়েডে ImageView-এর কোণসমূহ গোলাকার করা

অ্যান্ড্রয়েড অ্যাপে BitmapShader ব্যবহার করে ইমেজের কোণা গোলাকার করা

এই উদাহরণটি দেখাবে কীভাবে একটি সাধারণ ImageView-এ Bitmap এবং Canvas ব্যবহার করে রাউন্ডেড কর্নার (গোলাকার কোণা) প্রয়োগ করা যায়।

ধাপ ১: প্রজেক্ট তৈরি করুন

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:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_margin="10dp"
        android:layout_height="fill_parent" />

</LinearLayout>

ধাপ ৩: মেইন এক্টিভিটি (src/MainActivity.java)

package com.example.sample;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);
        textView.setTextColor(Color.RED);
        textView.setTextSize(20);

        ImageView mimageView = findViewById(R.id.imageView);

        // ড্রয়েবল থেকে Bitmap লিন
        Bitmap mbitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat)).getBitmap();

        // নতুন Bitmap তৈরি
        Bitmap imageRounded = Bitmap.createBitmap(mbitmap.getWidth(), mbitmap.getHeight(), mbitmap.getConfig());
        Canvas canvas = new Canvas(imageRounded);
        Paint mpaint = new Paint();
        mpaint.setAntiAlias(true);

        // BitmapShader ব্যবহার করে রাউন্ড রেক্ট আঁকুন
        mpaint.setShader(new BitmapShader(mbitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.drawRoundRect(new RectF(0, 0, mbitmap.getWidth(), mbitmap.getHeight()), 100, 100, mpaint);

        mimageView.setImageBitmap(imageRounded);
    }
}

ধাপ ৪: মেনিফেস্ট (app/manifests/AndroidManifest.xml)

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

দ্রষ্টব্য

  • R.drawable.cat আপনার drawable ফোল্ডারে থাকা একটি ছবির রেফারেন্স। আপনার প্রজেক্টে এই ছবি থাকতে হবে।
  • drawRoundRect-এর প্যারামিটার 100, 100 হলো কোণার ব্যাসার্ধ (radius)। প্রয়োজনে পরিবর্তন করুন।
  • আধুনিক অ্যান্ড্রয়েডে ShapeableImageView বা MaterialCardView ব্যবহার করা আরও সহজ ও পারফরম্যান্স-বান্ধব।

ফলাফল

অ্যাপ্লিকেশন রান করলে ইমেজটি গোলাকার কোণাসহ প্রদর্শিত হবে।

অ্যান্ড্রয়েডে ImageView-এর কোণসমূহ গোলাকার করা

  1. অ্যান্ড্রয়েড অ্যাপে ক্যামেরা ব্যবহার করার পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপে SurfaceView ট্রান্সপারেন্ট করবেন কীভাবে?

  3. অ্যান্ড্রয়েডে বাটনের কোণাবৃত্তাকার কর্নার তৈরি করার পদ্ধতি

  4. অ্যান্ড্রয়েড অ্যাপে সিম্পল টেক্সট ফাইল পড়ার পদ্ধতি