活动过渡滑动进/出动画

问题描述 投票:5回答:4

我知道这是互联网上的一些例子,而且在Stackoverflow上我发现了很多例子,但是不相信它们,没有一个能满足我的需求。 Even I have asked a similar question few time ago和我再次陷入这个问题。基本上是相同的问题,但方向相反。我可以用Activity B做任何我想要的动画,但这里的问题是Activity A,我可以在几个场景中制作动画。基本上ActivityA只在这个组合中玩enter_left

overridePendingTransition(R.anim.enter_from_right, R.anim.exit_on_left);

我想做的是在Activity AstartActivity()上制作(移动)onBackPressed(),而Activity B在屏幕上保持不动。 Activity A swould总是被绘制在顶部(作为滑动菜单,我可以用Activity B做到这一点)。我真的认为上面的代码片段可以完成这项工作:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
                startActivityForResult(intent, 500);
                overridePendingTransition(R.anim.stay_still, R.anim.exit_on_left);

但这甚至不播放任何动画

//this is the animation for onBackPressed()
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        overridePendingTransition(R.anim.enter_from_left, 0);
    }

我想要动画Activity A,但Activity B突然从屏幕上消失,我想留下来(设置(R.anim.enter_from_left, R.anim.stay_still)什么都不做)。

我准备了所有5个必要的动画:

enter_from_left

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="500"
    android:fromXDelta="-100%"
    android:toXDelta="0%" />
</set>

exit_on_left

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="500"
    android:fromXDelta="0%"
    android:toXDelta="-100%" />
</set>

enter_from_right

    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="500"
        android:fromXDelta="100%"
        android:toXDelta="0%" />
   </set>

exit_on_right

    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="100%" />
   </set>

stay_still

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="0%" />
</set>

我已经尝试了很多组合,但没有它们起作用。你能告诉我这个动画是否可行,是否可以这样做?我会发布一张图片,以便更清楚我想做什么:

所以,第一步:在startActivity()ActivityA应该离开屏幕从左侧移动,Activity B shoull已经“在那里”,“在它下面”。

然后,onBackPressed() Acyivity B应该“回来”,从屏幕的左侧进入并重叠保持不动的ActivityB

android animation android-activity android-xml
4个回答
0
投票

不确定你是否尝试过这种组合,但是应该可行.-

startActivityForResult(intent, 500);
overridePendingTransition(R.anim.enter_from_left, R.anim.stay_still);

0
投票

我可以发现下一个与片段fragmentTransaction.setCustomAnimations(R.anim.stay_still,R.anim.exit_on_left);非常好用 而overridePendingTransition(R.anim.stay_still, R.anim.exit_on_left);不适用于活动。

另一方面相反,

overridePendingTransition(R.anim.enter_from_left, 0);fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, 0);

与片段无关的活动不起作用。最接近的方法是transaction.setCustomAnimations(R.anim.stay_still,R.anim.exit_on_left, R.anim.enter_from_left,R.anim.exit_on_right);

如果有人有任何其他解决方案,我仍然等着听到它,我们将不胜感激。


0
投票

5年后,但存在解决方案。这两个函数需要在活动中实现,该活动将覆盖默认转换。

@Override
public void startActivity(Intent intent) {
    super.startActivity(intent);
    overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}

@Override
public void startActivityForResult(Intent intent, int requestCode) {
    super.startActivityForResult(intent, requestCode);
    overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}

from_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:interpolator="@android:interpolator/accelerate_decelerate" android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

from_left_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>

-1
投票

我不知道我是否真的理解你想要的东西,但如果你想创建一个滑动菜单,你可以使用这个教程来实现它,它工作得很好 - 这是解决你的问题的另一种选择 - :

http://developer.android.com/training/implementing-navigation/nav-drawer.html

但你必须使用FragmentActivity而不是Activity。如果你想要一个例子,只需要它,我会发布它。祝好运。

© www.soinside.com 2019 - 2024. All rights reserved.