Android में ड्रॉएबल के रंग प्रोग्रामेटिकली बदलना
यह ट्यूटोरियल दिखाता है कि कैसे AnimationDrawable और GradientDrawable का उपयोग करके ड्रॉएबल के रंग डायनामिकली बदले जाते हैं, जिससे रनटाइम पर थीम बदलना या विजुअल इफेक्ट्स बनाना संभव होता है।
चरण 1: प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट में बैकग्राउंड सेट करें
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context=".MainActivity"
tools:ignore="InvalidId">
</LinearLayout>
चरण 3: एनिमेशन ड्रॉएबल स्टार्ट करें
src/MainActivity.java:
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.activity_main);
AnimationDrawable animationDrawable = (AnimationDrawable) layout.getBackground();
if (animationDrawable != null) {
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.setExitFadeDuration(4000);
animationDrawable.start();
}
}
}
चरण 4: एनिमेशन लिस्ट ड्रॉएबल
res/drawable/background.xml - दो ग्रेडिएंट के बीच एनिमेशन:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/gradient1"
android:duration="4000" />
<item
android:drawable="@drawable/gradient2"
android:duration="4000" />
</animation-list>
चरण 5: ग्रेडिएंट ड्रॉएबल्स परिभाषित करें
res/drawable/gradient1.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:startColor="#21d6d3"
android:endColor="#044fab" />
</shape>
res/drawable/gradient2.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:startColor="#517f95"
android:endColor="#933c94" />
</shape>
रंग प्रोग्रामेटिकली बदलने के आधुनिक तरीके
1. ColorFilter के साथ (सिंपल कलर चेंज)
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_icon); drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); imageView.setImageDrawable(drawable);
2. DrawableCompat के साथ (API 4+ सपोर्ट)
Drawable wrapped = DrawableCompat.wrap(drawable.mutate()); DrawableCompat.setTint(wrapped, Color.BLUE); DrawableCompat.setTintMode(wrapped, PorterDuff.Mode.SRC_IN);
3. GradientDrawable के प्रॉपर्टीज बदलें
GradientDrawable drawable = (GradientDrawable) ContextCompat.getDrawable(context, R.drawable.shape);
drawable.setColor(Color.GREEN);
// या ग्रेडिएंट के लिए
drawable.setColors(new int[]{Color.RED, Color.BLUE});
drawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
4. VectorDrawable में टिंट (सबसे अच्छा आइकन्स के लिए)
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_vector"
android:tint="@color/primary" />
चरण 6: मैनिफेस्ट
स्टैंडर्ड कॉन्फ़िगरेशन।
बेस्ट प्रैक्टिसेज
- हमेशा
.mutate()कॉल करें ताकि शेयर्ड ड्रॉएबल स्टेट न बदले - VectorDrawable और टिंट का उपयोग करें स्केलेबल आइकन्स के लिए
- ColorStateList का उपयोग करें स्टेट-डिपेंडेंट कलर्स के लिए (दबाया गया, फोकस्ड, आदि)
- थीम एट्रिब्यूट्स (?attr/colorPrimary) का उपयोग करें कंसिस्टेंट थीमिंग के लिए