簡體   English   中英

如何在 Android 中使用動畫效果滑動 Activity?

[英]How to slide Activity with Animation Effect in Android?

我正在開發一個 Android 應用程序。 我想在活動上實現這樣的動畫,如果我們離開activity A那么它應該向左滑動,而新的activity B應該從右側滑入。 再次,當我離開當前activity B它也應該向右滑動,而之前的activity A應該從左向右滑動。 這怎么可能?

順便說一句,我正在使用以下代碼,但它沒有做任何事情..

overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);

我為動畫幻燈片編寫了一些 XML 代碼,如下所示:

slide_left_in.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="@android:integer/config_shortAnimTime" />
</set>

slide_left_out.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="@android:integer/config_shortAnimTime" />

</set>

您的幫助將不勝感激。 謝謝!

對於以下動畫,您需要這4 個動畫 xml .....

right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

並將這些代碼與 AcitivityA 一起使用...

            intent = new Intent(this, AcitvityB.class);
            startActivity(intent);
            overridePendingTransition( R.anim.left_in, R.anim.left_out);

並在 AcitivityB 中使用這些作為 BACK

@Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition( R.anim.right_in, R.anim.right_out);

    }

輸出:-

○

注意:- 如果您需要相反的動畫,只需更改右側而不是左側,反之亦然......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM