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

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.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">
    <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="Animate background color" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Changing Background color of this view."
        android:textSize="36sp"
        android:textStyle="bold" />
</RelativeLayout>

चरण 3: एनिमेशन लॉजिक

src/MainActivity.java:

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

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);

        button.setOnClickListener(v -> {
            ColorDrawable[] colorDrawables = {
                new ColorDrawable(Color.RED),
                new ColorDrawable(Color.BLUE),
                new ColorDrawable(Color.GREEN)
            };
            TransitionDrawable transitionDrawable = new TransitionDrawable(colorDrawables);
            textView.setBackground(transitionDrawable);
            transitionDrawable.startTransition(2000); // 2 सेकंड
        });
    }
}

चरण 4: मैनिफेस्ट

मानक मैनिफेस्ट।

व्याख्या

  • ColorDrawable - ठोस रंग ड्रॉएबल
  • TransitionDrawable - ड्रॉएबल सरणी के बीच क्रॉस-फेड एनिमेशन
  • startTransition(ms) - मिलीसेकंड में अवधि
  • आधुनिक विकल्प: ObjectAnimator या ValueAnimator के साथ backgroundColor प्रॉपर्टी

वैकल्पिक: ObjectAnimator दृष्टिकोण (API 21+)

ObjectAnimator animator = ObjectAnimator.ofObject(view, "backgroundColor",
    new ArgbEvaluator(), Color.RED, Color.BLUE);
animator.setDuration(2000);
animator.start();
  1. एंड्रॉइड में ऑप्शन मेनू का बैकग्राउंड रंग कैसे बदलें एंड्रॉइड में ऑप्शन मेनू का बैकग्राउंड रंग कैसे बदलें

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

  1. Outlook में कैलेंडर का बैकग्राउंड रंग कैसे बदलें Outlook में कैलेंडर का बैकग्राउंड रंग कैसे बदलें

    Outlook कैलेंडर एक शेड्यूलिंग घटक है जो ईमेल, संपर्क और अन्य सुविधाओं के साथ विलय हो गया है। यदि आप अपने Outlook कैलेंडर के वर्तमान बैकग्राउंड को पसंद नहीं करते हैं, तो आप हमेशा रंग को किसी भिन्न रंग में बदल सकते हैं। आप अपने कैलेंडर को विशिष्ट भी बना सकते हैं, खासकर यदि आप Outlook में एकाधिक कैलेंड

  1. iOS पर नोट्स की पृष्ठभूमि रंग कैसे बदलें iOS पर नोट्स की पृष्ठभूमि रंग कैसे बदलें

    यदि आप iOS में नोट्स ऐप का उपयोग कर रहे हैं, तो आप शायद इससे नफरत करते हैं कि यह आपके डिवाइस की सेटिंग के आधार पर एक सादा पृष्ठभूमि उपयोग करता है। यदि आपने लाइट मोड सक्षम किया है, तो आपको एक हल्की पृष्ठभूमि दिखाई देगी, और डार्क मोड का उपयोग करते समय एक डार्क पृष्ठभूमि। हालांकि, आप iOS में एक नोट की