سلام وقت بخیر چطوری میشه موقع عوض یا جا به جا شدن فرگمنت ها یک انیمیشن بدیم که جذاب تر بشه؟
سلام وقت بخیر
توی سرفصل انیمیشن های استاندارد و lottie با انیمیشن های پلتفرم اندروید آشنا میشن که توی کد جاوا اعمال میشن اما برای اعمال این انیمیشن ها روی فرگمت ها باید یکسری resource از نوع anim توی پوشه res پروژتون ایجاد کنید و انیمیشنتون رو داخل این فایل های xml تعریف کنید و در نهایت با استفاده از متد setCustomAnimations رو Transaction تون میتونید این انیمیشن ها رو اعمال کنید
//MainActivity
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_fragmentContainer, new FragmentA());
fragmentTransaction.commit();
findViewById(R.id.btn_main_goToA).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
fragmentTransaction.replace(R.id.frame_fragmentContainer, new FragmentB());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
//MainActivity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_main_goToA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/purple_500"
android:text="Go to b" />
<FrameLayout
android:id="@+id/frame_fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btn_main_goToA" />
</RelativeLayout>
//fragment_a
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="A"
android:textColor="@color/white"
android:textSize="42sp" />
</FrameLayout>
//fragment_b
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="B"
android:textColor="@color/white"
android:textSize="42sp" />
</FrameLayout>
//enter.xml in anim directory
// دقت کن این فایل باید داخل پوشه res/anim قرار بدی
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:toXDelta="0" />
</set>
//exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%" />
</set>
//pop_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
//pop_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%" />
</set>
و تامام
میتونید پروژه رو ران کنید و تست کنید فقط همانطور که گفتم باید سرفصل انیمیشن های استاندار اندروید رو بگذرونین تا درک درستی از این کدها داشته باشین البته برای تست نیازی به درک نیست
سلام وقت بخیر محمد جواد جان
ممنونم از پاسخ کامل میلاد عزیز
در سرفصل Navigation Component درمورد ایجاد انیمیشن هنگام جابجایی فرگمنت صحبت شده و بسیار ساده هست که پیش نیاز اصلی اون سرفصل برای ایجاد انیمیشن سرفصل انیمیشن های استاندارد در اندروید هست که باید بگذرونید.