परिचय
उदाहरण में जाने से पहले, हमें पता होना चाहिए कि ग्रेडिएंट कलर क्या है। विकिपीडिया के अनुसार, कंप्यूटर ग्राफिक्स में, एक कलर ग्रेडिएंट (कभी-कभी कलर रैम्प या कलर प्रोग्रेशन कहा जाता है) स्थिति-निर्भर रंगों की एक श्रृंखला निर्दिष्ट करता है, जिसका उपयोग आमतौर पर एक क्षेत्र को भरने के लिए किया जाता है। उदाहरण के लिए, कई विंडो मैनेजर स्क्रीन बैकग्राउंड को ग्रेडिएंट के रूप में निर्दिष्ट करने की अनुमति देते हैं।
यह उदाहरण दर्शाता है कि Android में एनिमेटेड ग्रेडिएंट बैकग्राउंड कैसे बनाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में लेआउट जोड़ें
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout 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/constraintLayout"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@drawable/gradient_animation"
tools:context = ".MainActivity">
<!-- Your layout here -->
<TextView
android:layout_width = "368dp"
android:layout_height = "520dp"
android:layout_marginBottom = "8dp"
android:layout_marginLeft = "8dp"
android:layout_marginRight = "8dp"
android:layout_marginTop = "8dp"
android:gravity = "center"
android:text = "@string/app_name"
android:textAlignment = "center"
android:textColor = "@android:color/background_light"
android:textSize = "30sp"
android:textStyle = "bold"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
tools:text = "@string/app_name"/>
</android.support.constraint.ConstraintLayout>
उपरोक्त कोड में हमने ड्रॉएबल में gradient_animation को बैकग्राउंड के रूप में जोड़ा है। अब ड्रॉएबल फोल्डर में gradient_animation.xml बनाएं और निम्नलिखित कोड जोड़ें:
<?xml version = "1.0" encoding = "utf-8"?>
<animation-list xmlns:android = "https://schemas.android.com/apk/res/android">
<item
android:drawable = "@drawable/drawable_purple_gradient"
android:duration = "3000" />
<item
android:drawable = "@drawable/drawable_amber_gradient"
android:duration = "3000" />
<item
android:drawable = "@drawable/drawable_green_gradient"
android:duration = "3000" />
<item
android:drawable = "@drawable/drawable_red_gradient"
android:duration = "3000" />
</animation-list>
उपरोक्त एनिमेशन-सूची में, हमने 4 चाइल्ड ड्रॉएबल जोड़े और प्रत्येक एनिमेशन व्यू के लिए अवधि जोड़ी, टाइमआउट के बाद, यह बैकग्राउंड बदल देगा। drawable_purple_gradient में बैंगनी रंग का बैकग्राउंड होता है, इसलिए drawable_purple_gradient.xml नामक एक फाइल ड्रॉएबल फोल्डर में बनाएं और निम्नलिखित कोड जोड़ें:
<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "https://schemas.android.com/apk/res/android">
<gradient
android:angle = "90"
android:endColor = "#D500F9"
android:startColor = "#4A148C" />
</shape>
उपरोक्त चरण का पालन करते हुए, ड्रॉएबल फोल्डर में drawable_amber_gradient.xml, drawable_green_gradient.xml और drawable_red_gradient.xml बनाएं और निम्नलिखित कोड जोड़ें:
drawable_amber_gradient.xml
<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "https://schemas.android.com/apk/res/android">
<gradient
android:angle = "135"
android:endColor = "#FFC400"
android:startColor = "#FF6F00" />
</shape>
drawable_green_gradient.xml
<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "https://schemas.android.com/apk/res/android">
<gradient
android:angle = "0"
android:endColor = "#00E676"
android:startColor = "#1B5E20"/>
</shape>
drawable_red_gradient.xml
<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "https://schemas.android.com/apk/res/android">
<gradient
android:angle = "45"
android:endColor = "#FF1744"
android:startColor = "#B71C1C" />
</shape>
चरण 3: MainActivity.java में कोड जोड़ें
package com.example.andy.myapplication;
import android.graphics.drawable.AnimationDrawable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout constraintLayout;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(3000);
animationDrawable.setExitFadeDuration(2000);
}
@Override
protected void onResume() {
super.onResume();
if (animationDrawable != null && !animationDrawable.isRunning()) {
animationDrawable.start();
}
}
@Override
protected void onPause() {
super.onPause();
if (animationDrawable != null && animationDrawable.isRunning()) {
animationDrawable.stop();
}
}
}
ऐप चलाएं
मान लें कि आपने अपना वास्तविक Android मोबाइल डिवाइस कंप्यूटर से कनेक्ट किया है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। अपने मोबाइल डिवाइस को विकल्प के रूप में चुनें और फिर अपना मोबाइल डिवाइस जांचें जो डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा:



जैसा कि उपरोक्त परिणाम में दिखाया गया है, हर 3 सेकंड में यह बैकग्राउंड कलर बदल देगा।