2010-04-19 69 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

相關問題