Canvas এবং Paint ব্যবহার করে কাস্টম ড্রয়িং: লাইন আঁকা
এই উদাহরণে দেখানো হয়েছে কীভাবে Canvas, Paint এবং Bitmap ব্যবহার করে প্রোগ্রামেটিক্যালি একটি লাইন আঁকা যায় এবং ImageView-এ দেখানো যায়।
প্রথম ধাপ: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
দ্বিতীয় ধাপ: লেআউট ফাইল
res/layout/activity_main.xml - একটি ImageView (ক্যানভাসের আউটপুট দেখানোর জন্য) এবং একটি বাটন:
<?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"
android:id="@+id/relativeLayout"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Line"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"/>
</RelativeLayout>
তৃতীয় ধাপ: মেইন এক্টিভিটিতে ড্রয়িং কোড
src/MainActivity.java - Bitmap তৈরি করে Canvas-এ লাইন আঁকা:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Context context;
Resources resources;
RelativeLayout relativeLayout;
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
resources = getResources();
relativeLayout = findViewById(R.id.relativeLayout);
button = findViewById(R.id.button);
imageView = findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ১. Bitmap তৈরি (প্রস্থ ১০px, উচ্চতা ৭০০px)
Bitmap bitmap = Bitmap.createBitmap(10, 700, Bitmap.Config.ARGB_8888);
// ২. Bitmap-এ আঁকার জন্য Canvas তৈরি
Canvas canvas = new Canvas(bitmap);
// ৩. ব্যাকগ্রাউন্ড রঙ দিন (ঐচ্ছিক)
canvas.drawColor(Color.RED);
// ৪. Paint অবজেক্ট কনফিগার
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE); // শুধু আউটলাইন
paint.setStrokeWidth(8); // লাইনের মোটা ৮ পিক্সেল
paint.setAntiAlias(true); // এন্টি-এলিয়াসিং (স্মুথ এজ)
// ৫. লাইন আঁকা: drawLine(startX, startY, stopX, stopY, paint)
int offset = 50;
canvas.drawLine(
offset, canvas.getHeight() / 2, // শুরু: (৫০, মাঝপথ)
canvas.getWidth() - offset, canvas.getHeight() / 2, // শেষ: (প্রস্থ-৫০, মাঝপথ)
paint
);
// ৬. ImageView-এ Bitmap সেট
imageView.setImageBitmap(bitmap);
}
});
}
}
চতুর্থ ধাপ: ম্যানিফেস্ট ফাইল
<?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>
Canvas ও Paint এর মূল মেথডসমূহ
| মেথড | ব্যবহার |
|---|---|
drawLine(x1, y1, x2, y2, paint) | দুই বিন্দুর মাঝে সরাসরি লাইন |
drawRect(rect, paint) | আয়ত আঁকা |
drawCircle(cx, cy, radius, paint) | বৃত্ত আঁকা |
drawText(text, x, y, paint) | টেক্সট আঁকা |
drawPath(path, paint) | কাস্টম পাথ আঁকা |
Paint.Style অপশনস
FILL- ভর করা (ডিফল্ট)STROKE- শুধু আউটলাইনFILL_AND_STROKE- উভয়ই
বेहतर পদ্ধতি: Custom View ক্লাস
বারবার বাটন ক্লিকে ড্র করার বদলে কাস্টম View বানান:
public class LineView extends View {
Paint paint = new Paint();
public LineView(Context context) {
super(context);
init();
}
private void init() {
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(50, getHeight()/2, getWidth()-50, getHeight()/2, paint);
}
}
তারপর লেআউটে <com.example.LineView ... /> ব্যবহার করুন।
অ্যাপ্লিকেশন চালান
"Draw Line" বাটনে ক্লিক করলে ImageView-এ একটি লাল লাইন দেখাবে।
