android活动切换方向

当在两个活动之间切换时,屏幕从右到左滑动.当我按后退键时,屏幕从右向左滑动.当我从活动按键返回屏幕滑动方向时,这是一种方式吗?

解决方法:

丹尼的解决方案可以起作用,但它过于复杂.您想要了解的关键方法是overridePendingTransition().

这是我嘲笑使用它的主要活动.我让它垂直过渡只是为了表明你可以在你喜欢的任何方向上进行变换:

package com.superliminal.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ScreenTransitionTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnForwards = (Button) findViewById(R.id.btnForwards);
        btnForwards.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity currentActivity = (Activity) v.getContext();
                Intent i = new Intent(currentActivity, NewScreen.class);
                // Tell the new activity how return when finished.
                i.putExtra("anim id in", R.anim.down_in);
                i.putExtra("anim id out", R.anim.down_out);
                currentActivity.startActivity(i);
                // This makes the new screen slide up as it fades in
                // while the current screen slides up as it fades out.
                overridePendingTransition(R.anim.up_in, R.anim.up_out);
            }
        });
    }
}

这是新屏幕的实现:

package com.superliminal.test;

import android.app.Activity;
import android.os.Bundle;

public class NewScreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_screen);
    }

    @Override
    public void onBackPressed() {
        this.finish();
        // Use exiting animations specified by the parent activity if given
        // Translate left if not specified.
        overridePendingTransition(
            getIntent().getIntExtra("anim id in", R.anim.left_in),
            getIntent().getIntExtra("anim id out", R.anim.left_out));
    }
}

您的布局文件可以是您喜欢的任何内容.您不需要包装层.这是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#990000" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Main Activity" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnForwards"
        android:text="Forward" />
</LinearLayout>

这是我的new_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#009900" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="New Screen. Use back button to return." />
</RelativeLayout>

您需要的唯一其他内容是放在res / anim文件夹中的动画XML文件.

up_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="1000"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>

up_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
</set>

down_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="1000"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>

down_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="1000"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
</set>
上一篇:Final Project 期末项目: PySnake


下一篇:Python编写坦克大战(新增无敌模式)-附赠源码