繁体   English   中英

我如何在 Android 中制作动态翻转屏幕(如 iPhone 的)

[英]How can i make a dynamic flipping screen(like that of iPhone) in Android

我正在通过网络服务解析数据。 我想要水平翻转而不是垂直翻转。 这是一个使用 ViewFlipper 的教程,但它用于静态数据。


这是我们需要在 2 个活动之间切换的代码:

飞溅.java

public class Splash extends Activity{

        /** Called when the activity is first created. */

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

                    startActivity(new Intent(Splash.this, MainMenu.class));
                    Splash.this.finish();                                     
        }
    }

飞溅.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splash">
</AbsoluteLayout>

菜单.java

public class Menu extends Activity{

        /** Called when the activity is first created. */

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);               
            setContentView(R.layout.menu);                                       
        }
    }

菜单文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/menu">
</AbsoluteLayout>

您可以使用 addView 动态地将页面添加到 ViewFlipper。

  flipper= (ViewFlipper) findViewById(R.id.flipper1);
  flipper.addView(myView,myViewIndex);

其中 myView 是您要添加的视图,而 myViewIndex 是您要添加此新视图的 viewflipper 中的索引。

然后,您可以将动画设置为在更改视图时执行:

  flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
  flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));

然后翻到这个页面,你可以使用:

  flipper.setDisplayedChild(myViewIndex);

其中 left_in.xml 定义为

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
  android:interpolator="@android:anim/accelerate_interpolator">
    <translate
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromXDelta="100%p"
    android:toXDelta="0" 
    android:duration="300"
    />
</set>

和 left_out.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"
    >
    <translate
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromXDelta="0" 
    android:toXDelta="-100%p" 
    android:duration="300"
    />
</set>

刚刚快速找了找你,因为我确定我以前见过它,我在 developer.android.com 上找到了这个

public void overridePendingTransition (int enterAnim, int exitAnim)

Since: API Level 5
Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
Parameters
enterAnim   A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim    A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.

因此,您可以将此额外参数添加到您的意图中,这将定义您调用的活动在新活动进入和旧活动退出时将如何动画化。

如果您想在两个活动之间导航而不使用视图翻转器,您可以使用普通意图。 写了一个教程,这样你就可以动画你的活动进出,

享受:

http://blog.blundellapps.co.uk/animate-an-activity/

您是否只希望屏幕根据其保持的位置(由加速度计传感器确定)旋转?

如果是这样,只需添加

android:screenOrientation="sensor"

到您的 AndroidManifest.xml 文件中的 Activity 节点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM