2010-04-19 73 views
6

我通过网络服务解析数据。我想要水平翻转而不是垂直翻转。这里是一个教程,其中使用ViewFlipper,但它是用于静态数据。如何在Android中制作动态翻转屏幕(如iPhone)


这里是我们的代码,我们需要2个活动之间翻转:

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

Splash.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> 

Menu.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);          
     } 
    } 

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> 
+2

你是什么意思“这是第一个使用ViewFlipper教程”? – 2011-12-20 14:17:56

+0

检查这个[答案](http://stackoverflow.com/questions/2651360/how-to-provide-animation-when-calling-another-activity-in-android/2651890#2651890) – Praveen 2010-04-19 13:59:44

回答

18

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

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

其中myView是您希望添加的视图,而myViewIndex是您要添加此新视图的视图片添加器中的索引。

然后,您可以设置动画瓶坯上的看法发生变化:

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> 

and left_out.xml is:

<?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> 
+0

假设我们有两个活动, > menu.xml 现在我们如何在这两种活动之间进行切换?这是从飞溅到菜单,反之亦然。 – 2010-04-20 06:18:00

+0

如果他们是两个不同的活动,那么这是一个完全不同的问题,因为你不会使用ViewFlipper。 如果它们只是在XML中定义的两个不同视图,那么您只需将它们添加到viewflipper并使用setDisplayedChild在它们之间翻转。 – stealthcopter 2010-04-20 08:47:02

+0

我明白了!所以有可能让iPhone像在Android中的2个不同的活动之间翻转。鉴于上述情况,您能否保留所需的代码?因为这是我们第一次做的事情。 – 2010-04-20 09:43:57

-4

你只是想在屏幕上转动的基础关闭它在正在举行(由加速度计传感器确定)的位置?

如果是的话只需要添加

android:screenOrientation="sensor" 

在AndroidManifest.xml中的活动节点文件

+0

与加速度计无关。我想让我的页面/屏幕像iPhone一样翻转。 – 2010-04-19 14:05:34

+0

那么你问一个翻转动画呢?我的iPhone体验是有限的,所以当你说“像在iPhone上翻转一样”时,我不完全清楚。 – snctln 2010-04-21 14:29:48

3

刚刚有了一个快速浏览一下你,因为我相信我绕前已经看到了,我发现this上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. 

所以,你可以将此额外的参数添加到您的意图,它将定义您所调用的活动将如何在新活动的入口和旧活动的退出时生成动画。

+0

正确,并且像魅力一样工作..更多信息,请访问http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/Animation.html 一件重要的事情。 。这个方法也被stealthcopter提到的API Level 5及以上版本支持。 – 2010-05-10 06:10:29

相关问题