android Activity 切换方向

标签 android screen slide direction

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

最佳答案

是的,这是可能的。在捕获后退键事件或导致新屏幕加载的任何其他事件后,您可以控制屏幕滑动的方向,但直观上如何操作并不明显。布局的最外层永远不会由您的代码进行动画处理,因此您可能需要将布局放在包装器中。您还必须从您希望动画发生的屏幕的 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 Activity 中单击“向后”按钮时动画的 ScreenTransitionsLab Activity :

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 Activity 中单击“前进”按钮时动画化的 NewScreen Activity :

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

}

实际执行动画的 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);

    }

}

关于android Activity 切换方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5105126/

相关文章:

android - 关于在一个 Activity 中使用两个 baseAdapter 切换布局

jquery - 监视器显示设置的边距动画问题

java - Ffmpeg 命令未在 Mac OS 上使用 Java RunTime 命令执行

Android xhdpi密度和分辨率

javascript - 执行某种淡入淡出的功能所涉及的机制/算法是什么?

Javascript 幻灯片滑动

javascript - 在图像中显示 bxslider

android - DownloadManager setVisibleInDownloadsUi 不起作用

android - 在特定时间停止服务

android - 复制资源/android/icon/mdpi-foreground.png 时出错