android,动画切换界面 animation

效果图



往左滑动屏幕,显示第一页,往右滑显示第二页

xml布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- 滑动屏幕专用的ViewFlipper -->

    <ViewFlipper
        android:id="@+id/viewfilp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <!-- 第一页 -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff"
            android:orientation="horizontal" >

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#339966" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="第二页" />
        </LinearLayout>
    </ViewFlipper>

</LinearLayout>
package ll.e.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
	private float startx;
	private ViewFlipper vf;
	private Animation inAnimation;
	private Animation outAnimation;
	private Animation yioutAnimation;
	private Animation yienterAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		vf = (ViewFlipper) findViewById(R.id.viewfilp);
		inAnimation = AnimationUtils.loadAnimation(this, R.anim.enteralpha);

		outAnimation = AnimationUtils.loadAnimation(this, R.anim.chutranslar);
		yienterAnimation = AnimationUtils.loadAnimation(this,
				R.anim.yientrtranslar);

		yioutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.yiouttranslar);
	}

	// public void click(View v) {
	// Intent intent = new Intent(this, OtherActivity.class);
	// startActivity(intent);
	// this.overridePendingTransition(R.anim.enteralpha, R.anim.exitalpha);
	// }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * 用来判断是想左滑,还是想右滑
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			startx = event.getX();

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

				vf.setInAnimation(inAnimation);//设置进入布局的风格
				vf.setOutAnimation(outAnimation);//设置出去布局的风格
				vf.showNext();//显示下一个控件
			} else if (endx < startx) {
				vf.setInAnimation(yienterAnimation);
				vf.setOutAnimation(yioutAnimation);
				vf.showPrevious();//显示上一个布局
			}
			return true;
		}
		return super.onTouchEvent(event);
	}

}

源码链接如下

http://download.csdn.net/detail/u014469691/9463703



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值