TransitionManager और Slide ट्रांजिशन का उपयोग करके व्यू को स्लाइड एनिमेशन के साथ दिखाने/छिपाने का तरीका।
चरण 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/btnSlideLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Slide Left" />
<Button
android:id="@+id/btnSlideRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnSlideLeft"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Slide Right" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cristiano Ronaldo is the Best Player in the World"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="invisible" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerInParent="true"
android:text="Messi is the Best Player in the World"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="invisible" />
</RelativeLayout>
चरण 3: स्लाइड एनिमेशन लॉजिक
src/MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Slide;
import android.transition.TransitionManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnSlideLeft, btnSlideRight;
TextView textView, textView1;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView1 = findViewById(R.id.textView1);
btnSlideLeft = findViewById(R.id.btnSlideLeft);
btnSlideRight = findViewById(R.id.btnSlideRight);
relativeLayout = findViewById(R.id.relativeLayout);
btnSlideLeft.setOnClickListener(v -> {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.START); // बाएं से
TransitionManager.beginDelayedTransition(relativeLayout, slide);
textView.setVisibility(View.VISIBLE);
});
btnSlideRight.setOnClickListener(v -> {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.END); // दाएं से
TransitionManager.beginDelayedTransition(relativeLayout, slide);
textView1.setVisibility(View.VISIBLE);
});
}
}
मुख्य क्लासेज
TransitionManager.beginDelayedTransition()- लेआउट परिवर्तनों को एनिमेट करता हैSlide- स्लाइड ट्रांजिशन (API 21+)Gravity.START/END/TOP/BOTTOM- स्लाइड दिशाFade,Explode- अन्य बिल्ट-इन ट्रांजिशन
कस्टम ट्रांजिशन XML (res/transition/slide.xml)
<slide xmlns:android="https://schemas.android.com/apk/res/android"
android:slideEdge="start"
android:duration="300" />
Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.slide); TransitionManager.beginDelayedTransition(container, transition);
नोट
यह API 21 (Lollipop) से उपलब्ध है। पुराने API के लिए Animation / Animator फ्रेमवर्क उपयोग करें।