Android में ImageView पर TextView ओवरले करके टेक्स्ट दिखाना
यह उदाहरण दर्शाता है कि एक इमेज के ऊपर TextView को पोजीशन करके "इमेज पर टेक्स्ट" प्रभाव कैसे बनाया जाए। वास्तविक बिटमैप ड्रॉइंग के लिए Canvas का उपयोग करना बेहतर होता है, लेकिन यह सरल लेआउट दृष्टिकोण UI ओवरले के लिए उपयुक्त है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (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"
android:padding="8dp"
tools:context=".MainActivity">
<!-- बैकग्राउंड इमेज -->
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/your_image"
android:contentDescription="बैकग्राउंड इमेज"/>
<!-- इमेज पर ओवरले टेक्स्ट -->
<TextView
android:id="@+id/overlayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="यह वह जगह है जहां मैं रहना चाहता हूं"
android:textSize="24sp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:background="#80000000"
android:borderRadius="8dp"/>
</RelativeLayout>
चरण 3: MainActivity (src/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 {
private TextView overlayText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
overlayText = findViewById(R.id.overlayText);
// डायनामिकली टेक्स्ट और स्टाइल सेट करें
overlayText.setText("यह वह जगह है जहां मैं रहना चाहता हूं");
overlayText.setTextColor(Color.WHITE);
overlayText.setTextSize(24);
// वैकल्पिक: बैकग्राउंड के लिए ड्रॉएबल सेट करें
// overlayText.setBackgroundResource(R.drawable.text_background);
}
}
वैकल्पिक: Canvas से वास्तविक बिटमैप पर टेक्स्ट ड्रॉ करना
अगर आपको इमेज फ़ाइल में स्थायी रूप से टेक्स्ट "बेक" करना है (शेयरिंग/सेविंग के लिए):
public Bitmap drawTextOnBitmap(Bitmap sourceBitmap, String text, int x, int y,
int textColor, float textSize) {
// म्यूटेबल बिटमैप कॉपी बनाएं
Bitmap result = Bitmap.createBitmap(sourceBitmap.getWidth(),
sourceBitmap.getHeight(),
sourceBitmap.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(sourceBitmap, 0, 0, null);
Paint paint = new Paint();
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setTextAlign(Paint.Align.LEFT);
// टेक्स्ट ड्रॉ करें
canvas.drawText(text, x, y, paint);
return result;
}
चरण 4: 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>
तुलना: ओवरले vs Canvas ड्रॉइंग
| दृष्टिकोण | फायदे | नुकसान |
|---|---|---|
| TextView ओवरले | सरल, डायनामिक, एनिमेटेबल, सेलेक्टेबल | इमेज सेव करने पर टेक्स्ट शामिल नहीं |
| Canvas ड्रॉइंग | स्थायी, शेयरेबल इमेज फाइल बनती है | जटिल, मेमोरी इंटेंसिव, स्टेटिक |
ऐप चलाएं
ड्रॉएबल में your_image.jpg रखें, ऐप चलाएं, और इमेज के नीचे सेंटर में स्टाइल्ड टेक्स्ट देखें।
