Activity 切换动画和页面切换动画

喜夏-厌秋 提交于 2020-03-22 04:33:16
public class MainActivity extends Activity {
    private ViewFlipper viewFlipper;
    private float startX;
    private Animation in_lefttoright;
    private Animation out_lefttoright;
    private Animation in_righttoleft;
    private Animation out_righttoleft;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
        out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
        
        in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
        out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
        //ViewFlipper 中的每一个组件都被视为一个页面
        viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
    }
    
    
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            startX = event.getX();
        }else if(event.getAction()==MotionEvent.ACTION_UP){
            float endX = event.getX();
            if(endX > startX){
                //设置进入的动画
                viewFlipper.setInAnimation(in_lefttoright);
                //设置出去的动画
                viewFlipper.setOutAnimation(out_lefttoright);
                viewFlipper.showNext();//显示下一页
                
            }else if(endX < startX){
                viewFlipper.setInAnimation(in_righttoleft);
                viewFlipper.setOutAnimation(out_righttoleft);
                viewFlipper.showPrevious();//显示前一页
            }
            return true;
        }
        return super.onTouchEvent(event);
    }



    public void openActivity(View v){
        Intent intent = new Intent(this, OtherActivity.class);
        startActivity(intent);
        //为Activity设置切换动画
        this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);//实现Activity切换动画效果
    }
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
     <translate
        android:fromXDelta="-100%p" <!--   100%p 标示屏幕的宽度-->
        
        android:toXDelta="0"
        
        android:duration="5000"
         />
</set>
<?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   <!--ViewFlipper 中的每一个组件都被视为一个显示页面-->
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/viewFlipper"
    >
        <!-- 第一页 -->
        <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          >
            <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="第一页"
            /> 
            <Button  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="打开新Activity"

                android:onClick="openActivity"
                />
        </LinearLayout>
        <!-- 第二页 -->
        <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#339900"
          >
           <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="第二页"
            /> 
       </LinearLayout>
    </ViewFlipper>

</LinearLayout>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!