android活动切换方向

时间:2011-02-24 13:12:54

标签: android screen slide direction

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

2 个答案:

答案 0 :(得分:5)

是的,有可能。捕获后退键事件或导致新屏幕加载的任何其他事件后,您可以控制屏幕滑动的方向,但对于如何执行此操作并不直观。布局的最外层将永远不会被您的代码设置为动画,因此您可能需要将布局放在包装器中。您还必须从屏幕的onCreate()内部调用动画代码,以便进行动画制作。

如果您使用“android.example”作为包使自己成为名为“ScreenTransitionLab”的项目,则可以使用以下内容来创建一个有助于您了解如何完成所需操作的工作示例。它目前设置为顶部和底部过渡,但可以轻松修改以使用左右过渡。

主屏幕已修改,因此整个屏幕将会滑动:

<?xml version="1.0" encoding="utf-8"?>
 <!--
  Wrapper layout whose children are to be animated. The outermost layout
  used by an activity can never be animated, so this wrapper is needed.
  The wrapper layout is given a different color so it can be
  distinguished from the layout that is animated.
 -->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="#0000FF"
>
 <!-- Actual layout that is animated. -->
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FF0000"
 >
  <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello" />
  <Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/btnForwards"
   android:text="Forwards" />
 </LinearLayout>
</LinearLayout>

修改了新屏幕,以便整个屏幕都会滑动:

<?xml version="1.0" encoding="utf-8"?>
 <!--
  Wrapper layout whose children are to be animated. The outermost layout
  used by an activity can never be animated, so this wrapper is needed.
  The wrapper layout is given a different color so it can be
  distinguished from the layout that is animated.
 -->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="#0000FF"
>
 <!-- Actual layout that is animated. -->
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FF0000"
 >
  <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello" />
  <Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/btnForwards"
   android:text="Forwards" />
 </LinearLayout>
</LinearLayout>

在NewScreen活动中单击“向后”按钮时动画的ScreenTransitionsLab活动:

package android.example;

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

public class ScreenTransitionLab extends Activity {
    // Layout fields
    protected LinearLayout mainLayout;
    public static Button btnForwards = null;
    public static Activity currentActivity;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentActivity = this;

        /*
         * This creates View objects from the xml file. The xml file should
         * define all views and all static attributes.
         */
        mainLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
                null);

        btnForwards = (Button) mainLayout.findViewById(R.id.btnForwards);
        btnForwards.setOnClickListener(forwardsOnClickListener);

        UIHelper.setSlideDirection(mainLayout, UIHelper.bottom);

        /*
         * Use the Layout that contains the View objects that were modified to
         * create screen that will be shown after activity is done processing
         * instead of the xml file. The Layout will contain all of the views and
         * static attributes that were defined in the xml file plus all of the
         * dynamic attributes that were defined in the code above.
         */
        setContentView(mainLayout);
    }

    public View.OnClickListener forwardsOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            Activity currentActivity = (Activity) v.getContext();
            Intent i = new Intent(currentActivity, NewScreen.class);
            currentActivity.startActivity(i);

            /*
             * Remove activity that is no longer current from the activity stack
             * to prevent the application from bloating.
             */
            currentActivity.finish();
        }
    };

}

在ScreenTransitionsLab活动中点击“转发”按钮时动画的NewScreen活动:

package android.example;

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

public class NewScreen extends Activity {
    protected LinearLayout mainLayout;
    public static Button btnBackwards = null;
    public static Activity currentActivity;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentActivity = this;

        /*
         * This creates View objects from the xml file. The xml file should
         * define all views and all static attributes.
         */
        mainLayout = (LinearLayout) getLayoutInflater().inflate(
                R.layout.new_screen, null);

        btnBackwards = (Button) mainLayout.findViewById(R.id.btnBackwards);
        btnBackwards.setOnClickListener(backwardsOnClickListener);

        UIHelper.setSlideDirection(mainLayout, UIHelper.top);

        /*
         * Use the Layout that contains the View objects that were modified to
         * create screen that will be shown after activity is done processing
         * instead of the xml file. The Layout will contain all of the views and
         * static attributes that were defined in the xml file plus all of the
         * dynamic attributes that were defined in the code above.
         */
        setContentView(mainLayout);
    }

    public View.OnClickListener backwardsOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            Activity currentActivity = (Activity) v.getContext();
            Intent i = new Intent(currentActivity, ScreenTransitionLab.class);
            currentActivity.startActivity(i);

            /*
             * Remove activity that is no longer current from the activity stack
             * to prevent the application from bloating.
             */
            currentActivity.finish();
        }
    };

}

实际执行aninmation的UIHelper类:

package android.example;

import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;

public class UIHelper {
    public static final int top = 1;
    public static final int bottom = 2;
    public static final int left = 3;
    public static final int right = 4;

    /**
     * Set direction that children in the panel will slide in from when next
     * displayed.
     * 
     * @param panel
     *            {@link ViewGroup} whose children will be slid in from the
     *            specified direction when the panel is next displayed.
     * @param fromDirection
     *            Primitive int indicating the direction to slide the children
     *            of the panel from.
     */
    public static void setSlideDirection(ViewGroup panel, int fromDirection) {
        float fromX = 0;
        float toX = 0;
        float fromY = 0;
        float toY = 0;

        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(100);
        set.addAnimation(animation);

        switch (fromDirection) {
        case top:
            fromX = 0.00f;
            toX = 0.00f;
            fromY = -1.00f;
            toY = 0.00f;
            break;
        case bottom:
            fromX = 0.00f;
            toX = 0.00f;
            fromY = 1.00f;
            toY = 0.00f;
            break;
        case left:
            fromX = -1.00f;
            toX = 0.00f;
            fromY = 0.00f;
            toY = 0.00f;
            break;
        default:
            fromX = 1.00f;
            toX = 0.00f;
            fromY = 0.00f;
            toY = 0.00f;
            break;
        }

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
                Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
                fromY, Animation.RELATIVE_TO_SELF, toY);
        animation.setDuration(200);
        set.addAnimation(animation);

        LayoutAnimationController controller = new LayoutAnimationController(
                set, 0.25f);
        panel.setLayoutAnimation(controller);

    }

}

答案 1 :(得分:3)

Danny的解决方案可以起作用,但它过于复杂。您想要了解的关键方法是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>