ছবির ওপর টেক্সট ওভারলে করা
এই উদাহরণটি দেখায় কীভাবে একটি ImageView-এ ছবি দেখিয়ে তার ওপর TextView-এ টেক্সট রাখা যায়। এটি সাধারণত মেম জেনারেটর, ক্যাপশন অ্যাড্ডিং বা ভтерমার্ক অ্যাপে ব্যবহৃত হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML (activity_main.xml) - FrameLayout/RelativeLayout ব্যবহার
<?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:padding="8dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/image"
android:contentDescription="Background image" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:padding="8dp"
android:text="This is the place where I want to live"
android:textSize="24sp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:shadowColor="#000000"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="4" />
</RelativeLayout>
ধাপ ৩: MainActivity.java বাস্তবায়ন
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
textView.setText("This is the place where I want to live");
textView.setTextColor(Color.WHITE);
}
}
উন্নত পদ্ধতি: Canvas ব্যবহার করে টেক্সট আঁকা (Permanent)
যদি টেক্সটকে ছবির সাথে পারমানেন্টভাবে মিলিয়ে নতুন ছবি তৈরি করতে চান:
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
public Bitmap drawTextOnBitmap(Bitmap sourceBitmap, String text, int x, int y) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), sourceBitmap.getConfig());
Canvas canvas = new Canvas(resultBitmap);
// মূল ছবি আঁকুন
canvas.drawBitmap(sourceBitmap, 0, 0, null);
// টেক্সট পেইন্ট কনফিগার করুন
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(60);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setShadowLayer(4, 2, 2, Color.BLACK);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD_ITALIC));
// টেক্সট আঁকুন
canvas.drawText(text, x, y, paint);
return resultBitmap;
}
// ব্যবহার:
ImageView imageView = findViewById(R.id.imageView);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap newBitmap = drawTextOnBitmap(bitmap, "My Text", 100, 100);
imageView.setImageBitmap(newBitmap);
ধাপ ৪: 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>
টিপস এবং ট্রিকস
- শ্যাডো/আউটলাইন: টেক্সট পড়া যেতে শ্যাডো বা আউটলাইন ব্যবহার করুন
- গ্র্যাডিয়েন্ট ব্যাকগ্রাউন্ড: টেক্সটের পিছনে সেমি-ট্রান্সপারেন্ট বক্স যোগ করুন
- মাল্টি-লাইন: `StaticLayout` ব্যবহার করে মাল্টি-লাইন টেক্সট আঁকুন
- ছবি সংরক্ষণ: `compress()` মেথড দিয়ে নতুন বিটম্যাপ সেভ করুন
অ্যাপ্লিকেশন টেস্ট করুন
res/drawable ফোল্ডারে "image.jpg" নামে একটি ছবি রাখুন। অ্যাপ রান করলে ছবির নিচে সাদা রঙের টেক্সট দেখাবে। Canvas পদ্ধতিতে টেক্সট ছবির অংশ হয়ে যাবে।