एंड्रॉइड में लेयर-लिस्ट से दो इमेज ओवरले करना
यह उदाहरण दिखाता है कि <layer-list> ड्रॉएबल का उपयोग करके दो इमेज को मर्ज करके ImageView में कैसे प्रदर्शित किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML
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="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnOverlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="इमेज ओवरले करें" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnOverlay"
android:scaleType="centerInside" />
</RelativeLayout>
चरण 3: ड्रॉएबल फोल्डर में इमेज रखें
res/drawable/ फोल्डर में दो इमेज कॉपी करें (उदाहरण: image.png और image1.png)।
चरण 4: लेयर-लिस्ट XML बनाएं
res/drawable/layer.xml बनाएं:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image" />
<item android:drawable="@drawable/image1" />
</layer-list>
नोट: लेयर-लिस्ट में आइटम्स का क्रम मायने रखता है - पहला आइटम नीचे, अंतिम आइटम सबसे ऊपर होता है।
चरण 5: MainActivity में लॉजिक
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
findViewById(R.id.btnOverlay).setOnClickListener(this::overlayImages);
}
public void overlayImages(View view) {
imageView.setImageResource(R.drawable.layer);
}
}
चरण 6: Manifest
<?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>
उन्नत: प्रोग्रामेटिकली ओवरले करना
यदि आपको रनटाइम पर डायनामिकली इमेज ओवरले करनी है:
private Drawable overlayImages(Drawable bottom, Drawable top) {
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{bottom, top});
return layerDrawable;
}
// उपयोग:
Drawable bottom = ContextCompat.getDrawable(this, R.drawable.image);
Drawable top = ContextCompat.getDrawable(this, R.drawable.image1);
imageView.setImageDrawable(overlayImages(bottom, top));
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। बटन दबाने पर दोनों इमेज ओवरले होकर ImageView में दिखाई देंगी।