activity切換動畫和頁面切換動畫

jopen 9年前發布 | 11K 次閱讀 Android開發 移動開發 Activity

Activity切換動畫

要實現Activity切換動畫需要靠overridePendingTransition來實現,里面有兩個參數分別是進入Activity時的動畫和離開Activity時的動畫。

需要注意的是必須在StartActivity()或finish()之后立即調用

比如在MainActivity中有一個Button,點擊Button后跳轉到OtherActivity中代碼如下:

    Intent intent = new Intent(this, OtherActivity.class);  
    startActivity(intent);  
    this.overridePendingTransition(R.anim.enteralpha, R.anim.exitalpha);  

界面切換動畫

界面切換動畫要靠ViewFlipper來實現

    <ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <!-- 第一頁 -->  

    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="#009900"  
        android:orientation="vertical" >  

        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="第一頁" />  
    </LinearLayout>  
    <!-- 第二頁 -->  

    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="#ffff00"  
        android:orientation="vertical" >  

        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="第二頁" />  
    </LinearLayout>  
</ViewFlipper>  </pre><br />

    @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startX = event.getX();

    } else if (event.getAction() == MotionEvent.ACTION_UP) {  
        float endX = event.getX();  
        if (endX > startX ) {  

            flipper.showNext();// 顯示下一頁  

        } else if (endX<startX) {  

            flipper.showPrevious();// 顯示前一頁  
        }  
        return true;  
    }  

    return super.onTouchEvent(event);  
}  </pre><br />

這樣在手指左右滑動的時候切換頁面。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!