यह उदाहरण एक TextView से दूसरे में टेक्स्ट एनिमेट करने के बारे में है।
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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:textSize="30dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:textColor="#ff4500"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="@+id/animate"
android:text="Animated button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
उपरोक्त कोड में, हमने TextView और बटन लिया है। जब उपयोगकर्ता बटन पर क्लिक करता है, तो TextView अपडेट होगा।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
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);
final LinearLayout parent = findViewById(R.id.parent);
view.setText("1+2");
Button animate = findViewById(R.id.animate);
animate.setOnClickListener(v -> {
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
slideUp.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
view.setText("3");
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
parent.startAnimation(slideUp);
});
}
}
res/anim/slide_up.xml में स्लाइड अप एनिमेशन परिभाषित करें।
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। बटन पर क्लिक करने पर, TextView "1+2" से "3" में एनिमेशन के साथ अपडेट होगा।