本文介绍了overridePendingTransition在进出滑动顺畅活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法搞清楚如何在和滑出活动,按一下按钮。我想是推一个按钮,然后在屏幕上滑动用户。我想这是第一次活动(一个带按钮),以滑出的方式离开,而新的第2活动从右侧滑入。

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.

通过下面的code,单击该按钮时,第一个活动滑出的权利时,我希望它滑出左侧。然后,当它完成滑动,所有的剩余量是黑屏的一瞬间,然后在第二个活动只出现在不滑动。

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.

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

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.

1日活动

@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);
        }
    });

第二活性

@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);

所以,我在想,我的一些XML文件可能不正确。在这里,他们是。

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

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>

修改删除 overridePendingTransition()从第二个活动说得那么第一活动滑出左侧这正是我想要的。但是,当第一活动滑走,它仍然是刚刚露出从右侧具有在第二活动滑动的黑屏代替

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.

推荐答案

而不是覆盖在这两个动画 startActivity()和新活动的onCreate(),你只需要覆盖动画只是 startActivity()通话后。

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.

两个 INT 是你为 overridePendingTransition(INT enterAnim,诠释exitAnim)对应两个动画 - 删除旧活动并添加新的。

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

有关你的第二个问题,我相信你有fromXDelta设置错误,-100%应该是一路掉在左手屏幕的一侧,不正确的,那么改变这100%应该修复它。

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

这篇关于overridePendingTransition在进出滑动顺畅活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:28