Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके एंड्रॉइड में व्यू के बैकग्राउंड कलर परिवर्तन को कैसे एनिमेट करें?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड में व्यू के बैकग्राउंड कलर परिवर्तन को TransitionDrawable का उपयोग करके कैसे एनिमेट किया जाए।

चरण 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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView"
        android:layout_centerInParent="true"
        android:layout_marginBottom="15dp"
        android:text="बैकग्राउंड कलर एनिमेट करें" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="इस व्यू का बैकग्राउंड कलर बदल रहा है।"
        android:textAlignment="center"
        android:textColor="@android:color/background_dark"
        android:textSize="36sp"
        android:textStyle="bold" />

</RelativeLayout>

चरण 3: Kotlin कोड लागू करें

src/MainActivity.kt में निम्नलिखित कोड जोड़ें:

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.TransitionDrawable
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView
    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        textView = findViewById(R.id.textView)
        button = findViewById(R.id.button)
        
        button.setOnClickListener {
            val colorDrawables = arrayOf(
                ColorDrawable(Color.GREEN),
                ColorDrawable(Color.RED),
                ColorDrawable(Color.YELLOW)
            )
            val transitionDrawable = TransitionDrawable(colorDrawables)
            textView.background = transitionDrawable
            transitionDrawable.startTransition(2000) // 2 सेकंड में ट्रांजिशन
        }
    }
}

चरण 4: मैनिफेस्ट कॉन्फ़िगर करें

androidManifest.xml में निम्नलिखित कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.q11">

    <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>

ऐप चलाएं

अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।

  1. Android में View के बैकग्राउंड कलर बदलाव को एनिमेट कैसे करें? Android में View के बैकग्राउंड कलर बदलाव को एनिमेट कैसे करें?

    TransitionDrawable का उपयोग करके View के बैकग्राउंड कलर को स्मूथली एनिमेट करने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=https://schemas.android.c

  1. ActionBarActivity के एक्शन बार का बैकग्राउंड रंग कैसे बदलें? ActionBarActivity के एक्शन बार का बैकग्राउंड रंग कैसे बदलें?

    Android में एक्शन बार/टूलबार का बैकग्राउंड रंग बदलना यह ट्यूटोरियल दिखाता है कि कैसे थीम और स्टाइल्स का उपयोग करके एक्शन बार (या आधुनिक Toolbar) का बैकग्राउंड रंग कस्टमाइज़ किया जाता है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: बेसिक लेआउट res/layout/activity_main.xml:

  1. एंड्रॉइड में ऑप्शन मेनू का बैकग्राउंड रंग कैसे बदलें एंड्रॉइड में ऑप्शन मेनू का बैकग्राउंड रंग कैसे बदलें

    स्टाइल्स और थीम्स के माध्यम से ऑप्शन मेनू का रंग बदलना एंड्रॉइड में ऑप्शन मेनू (टूलबार/एक्शन बार मेनू) का बैकग्राउंड और टेक्स्ट रंग बदलने के लिए स्टाइल्स/थीम्स का उपयोग करना सबसे सही तरीका है। यह विधि पूरे ऐप में सुसंगत स्टाइलिंग सुनिश्चित करती है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File