Java overridePendingTransition 用于平滑地滑入和滑出活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20690764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:37:12  来源:igfitidea点击:

overridePendingTransition for sliding activities in and out smoothly

javaandroidandroid-activitytransition

提问by Matt

I'm having trouble figuring out how to slide activities in and out with a push of a button. What I want is for the user to push a button and then the screen slides. The way I want it is for the 1st activity (the one with the button) to slide out to the left while the new 2nd activity slides in from the right.

我无法弄清楚如何通过按下按钮来滑入和滑出活动。我想要的是让用户按下一个按钮,然后屏幕滑动。我想要的方式是第一个活动(带有按钮的活动)向左滑出,而新的第二个活动从右侧滑入。

With the below code, when the button is clicked, the 1st activity slides out to the right when I want it to slide out to the left. Then when it is done sliding, all that is remaining is a black screen for a split second and then the 2nd activity just appears and does not slide in.

使用下面的代码,当单击按钮时,第一个活动在我希望它向左滑出时向右滑出。然后当它完成滑动时,剩下的只是一瞬间的黑屏,然后第二个活动只是出现并且不会滑入。

So the 1st activity is sliding out the incorrect direction and the next activity just appears instead of sliding. What am I doing wrong? I am having a hard time understanding the XML files so hear is the code for everything below.

所以第一个活动滑出不正确的方向,下一个活动只是出现而不是滑动。我究竟做错了什么?我很难理解 XML 文件,所以听到的是下面所有内容的代码。

1st activity

第一次活动

@Override
public void onCreate(Bundle savedInstanceState) {

    playBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainMenu.this, Levels.class);
            startActivity(intent);
            overridePendingTransition(R.anim.enter_from_right, R.anim.exit_out_left);
        }
    });

2nd activity

第二个活动

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.levels);

    overridePendingTransition(R.anim.enter_from_left, R.anim.exit_out_right);

So I am thinking that some of my XML files might be incorrect. Here they are.

所以我认为我的一些 XML 文件可能不正确。他们来了。

enter_from_left.xml

enter_from_left.xml

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

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

enter_from_right.xml

enter_from_right.xml

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

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

exit_out_left.xml

exit_out_left.xml

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

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

exit_out_right.xml

exit_out_right.xml

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

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

EDITRemoving the overridePendingTransition()from the 2nd activity made it so the 1st activity slides out to the left which is what I wanted. But, when the 1st activity slides away, it still is just revealing a black screen instead of having the 2nd activity slide in from the right.

编辑overridePendingTransition()从第二个活动中 删除使得第一个活动向左滑出,这就是我想要的。但是,当第一个活动滑开时,它仍然只是显示一个黑屏,而不是让第二个活动从右侧滑入。

采纳答案by panini

Instead of overriding the animation in both startActivity()and the new activities onCreate(), you only need to override the animation just after the startActivity()call.

您只需在调用后立即覆盖动画,而不是在两者startActivity()和新活动onCreate()中覆盖动画startActivity()

The two ints you provide for overridePendingTransition(int enterAnim, int exitAnim)correspond to the two animations - removing the old Activityand adding the new one.

int您提供的两个soverridePendingTransition(int enterAnim, int exitAnim)对应于两个动画 - 删除旧动画Activity并添加新动画。

For your second question, I believe you have the fromXDelta set wrong, -100% should be all the way off the left-handside of the screen, not the right, so changing this to 100% should fix it.

对于您的第二个问题,我相信您的 fromXDelta 设置错误,-100% 应该一直远离屏幕的左侧,而不是右侧,因此将其更改为 100% 应该可以解决它。

回答by mircobabini

There's an error not only in the enter_from_right animation, that should have a fromXDelta of 100% instead of -100%, but even in the enter_from_left animation, that should fave a fromXDelta of -100% instead of 100%.

不仅在 enter_from_right 动画中存在错误,它的 fromXDelta 应该为 100% 而不是 -100%,而且即使在 enter_from_left 动画中,也应该有一个 fromXDelta 为 -100% 而不是 100%。

Cheers,

干杯,

回答by syedjibharat

Change fromXDeltato -100%from enter_from_leftand fromXDeltato 100%from enter_from_rightin your code, this will give you a correct sliding animation.

在您的代码中更改fromXDelta-100%fromenter_from_leftfromXDeltato 100%from enter_from_right,这将为您提供正确的滑动动画。

回答by Jagie

look at my gist, it works perfectly:

看看我的要点,它完美地工作:

1.Override CommonActivity's startActivity and finish

1.覆盖CommonActivity的startActivity和finish

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

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.from_left_in, R.anim.from_right_out);
    }

2.from_left_in.xml

2.from_left_in.xml

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

3.from_right_in.xml

3.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>

4.from_left_out.xml

4.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>

5.from_right_out.xml

5.from_right_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>

gist link: https://gist.github.com/JagieChen/f5cc44bf663f3722bd19097be47ccf9b

要点链接:https: //gist.github.com/JagieChen/f5cc44bf663f3722bd19097be47ccf9b

回答by Tom Peak

Don't forget the pivot at this point! fE. Move from up to down. pivotY is 100% that meen's bottom, so your 0% are on the bottom and -100% are up and you don't see it. Makes it more handy when you set the pivot to your border.

在这一点上不要忘记支点!fE。从上往下移动。pivotY 是 meen 底部的 100%,所以你的 0% 在底部,-100% 在上面,你看不到它。将枢轴设置为边框时更方便。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:shareInterpolator="false">
    <translate
            android:duration="800"
            android:fromYDelta="-100%"
            android:toYDelta="0%"
            android:interpolator="@android:anim/bounce_interpolator"
            android:pivotX="50%"
            android:pivotY="100%"/>
</set>