首页 > 代码库 > 界面切换动画效果

界面切换动画效果

private ViewFlipper flipper;    

private float startX;    

private Animation in_lefttoright;    

private Animation outlefttoright;        

private Animation in_righttoleft;    

private Animation out_righttoleft;    

@Override    

public void onCreate(Bundle savedInstanceState) {        

super.onCreate(savedInstanceState);        

setContentView(R.layout.main);                

flipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);                

in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.in_lefttoright);        

outlefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);                

in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.in_righttoleft);        

out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);    

}

 

@Override  

public boolean onTouchEvent(MotionEvent event) {   

  switch (event.getAction()) {   

    case MotionEvent.ACTION_DOWN:    

      startX = event.getX();    

      break;

      case MotionEvent.ACTION_UP:    

      float endX = event.getX();    

      if(endX > startX){

        //向右     

        flipper.setInAnimation(in_lefttoright);     

        flipper.setOutAnimation(outlefttoright);     

        flipper.showNext();

        //显示下一页    

      }else if(endX < startX){

        //向左     

        flipper.setInAnimation(in_righttoleft);     

        flipper.setOutAnimation(out_righttoleft);     

        flipper.showPrevious();    

      }    

      break;   

    }   

    return super.onTouchEvent(event);  

  }    

}

 

main.xml主布局文件:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <ViewFlipper 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/viewFlipper"
     >
     <!-- 第一页 -->
     <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
      <TextView
       android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="第一屏"
      />
      
     </LinearLayout>
     
     <!-- 第二页 -->
     <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#FFFFFF"
     >
      <TextView
       android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="第二屏"
      />
      
     </LinearLayout>
    </ViewFlipper>
</LinearLayout>

 

in_lefttoright.xml文件:

  

<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">  

<translate        

  android:fromXDelta="-100%p"                

  android:toXDelta="0"                

  android:duration="3000"         />

</set>

 

in_righttoleft.xml文件:

  

<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">  

<translate        

  android:fromXDelta="100%p"              

    android:toXDelta="0"                

  android:duration="3000"         />

</set>

 

out_lefttoright.xml文件:

  

<set xmlns:android="http://schemas.android.com/apk/res/android"    

  android:shareInterpolator="false">  

  <translate        

    android:fromXDelta="0"                

    android:toXDelta="100%p"                

    android:duration="3000"         />

</set>

out_righttoleft.xml文件:

 

  

<set xmlns:android="http://schemas.android.com/apk/res/android"    

  android:shareInterpolator="false">  

  <translate         android:fromXDelta="0"                

          android:toXDelta="-100%p"                

        android:duration="3000"         />

</set>