6

我正在通过 Web 服务解析数据。我想要水平翻转而不是垂直翻转。这是一个使用 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

<?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>
4

4 回答 4

18

您可以使用 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>
于 2010-04-19T21:23:34.800 回答
3

只是快速查找一下,因为我确定我以前见过它,我在 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.

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

于 2010-04-21T09:23:33.900 回答
1

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

享受:

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

于 2012-02-09T13:37:26.903 回答
-4

Do you just want the screen to rotate based off of the position it is being held in (as determined by the accelerometer sensors)?

If so just add

android:screenOrientation="sensor"

to the Activity node in your AndroidManifest.xml file

于 2010-04-19T13:56:10.080 回答