यह उदाहरण Android TextView में काउंट एनिमेशन बनाने के बारे में है।
काउंट एनिमेशन का उदाहरण
चरण 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:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textSize="20dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:textColor="#ff4500"
android:layout_height="wrap_content"
android:singleLine="true" />
</RelativeLayout>
उपरोक्त कोड में, हमने 0 से 100 तक काउंट एनिमेशन दिखाने के लिए TextView लिया है।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView view = findViewById(R.id.text);
ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.addUpdateListener(animation ->
view.setText(String.valueOf(animation.getAnimatedValue())));
animator.setDuration(10000); // एनिमेशन की अवधि सेट करें
animator.start();
}
}
काउंट एनिमेशन प्राप्त करने के लिए निम्नलिखित कोड उपयोग करें:
ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.addUpdateListener(animation ->
view.setText(String.valueOf(animation.getAnimatedValue())));
animator.setDuration(10000);
animator.start();
उपरोक्त कोड में, यह 0 से 100 तक 10000ms (10 सेकंड) के समय अंतराल में शुरू होता है।
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। परिणाम में, यह काउंट एनिमेशन के साथ टेक्स्ट को बदलेगा।