利用ViewFlipper和手势实现软件透明导航

package com.nd.activitydemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class ActivityDemoActivity extends Activity implements OnGestureListener {
	/** Called when the activity is first created. */

	GestureDetector mGestureDetector;
	// private static final int SCROLL_MIN_DISTANCE = 160;
	// private static final int SCROLL_MIN_VELOCITY = 10;
	private ViewFlipper vf;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Transparent);
		setContentView(R.layout.transparent);

		vf = (ViewFlipper) findViewById(R.id.vf);
		vf.addView(addImageById(R.drawable.guardpic1), new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		vf.addView(addImageById(R.drawable.guardpic2), new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		mGestureDetector = new GestureDetector(ActivityDemoActivity.this);
	}

	// 开发时对手势的测试
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		boolean retValue = mGestureDetector.onTouchEvent(event);
		int action = event.getAction();
		if (action == MotionEvent.ACTION_UP) {
			// Helper method for lifted finger
		} else if (action == MotionEvent.ACTION_CANCEL) {
		}
		return retValue;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub

		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		Log.i("Fling", "Fling Happened!");
		if (e1.getX() - e2.getX() > 120) {
			this.vf.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));

			this.vf.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			this.vf.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			this.vf.showPrevious();
			return true;
		}
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		Log.i("MotionEvent", "onScroll.........");
		// 参数解释:
		// e1:第1个ACTION_DOWN MotionEvent
		// e2:最后一个ACTION_MOVE MotionEvent
		// velocityX:X轴上的移动速度,像素/秒
		// velocityY:Y轴上的移动速度,像素/秒

		// 触发条件 :
		// X轴的坐标位移大于SCROLL_MIN_DISTANCE,且移动速度大于SCROLL_MIN_VELOCITY个像素/秒

		// if (e1.getX() - e2.getX() > SCROLL_MIN_DISTANCE
		// && Math.abs(distanceX) > SCROLL_MIN_VELOCITY) {
		// // Fling left
		// iv.setBackgroundResource(R.drawable.guardpic2);
		// } else if (e2.getX() - e1.getX() > SCROLL_MIN_DISTANCE
		// && Math.abs(distanceX) > SCROLL_MIN_VELOCITY) {
		// // Fling right
		// iv.setBackgroundResource(R.drawable.detailpageguideinfo);
		// }
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		ActivityDemoActivity.this.finish();
		return false;
	}

	public View addImageById(int id) {
		ImageView iv = new ImageView(this);
		iv.setImageResource(id);
		return iv;
	}
}



布局文件:

<?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:id="@+id/vf"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    />
</LinearLayout>


参考资料:

ViewFlipper Animation 使用 http://www.iteye.com/topic/569493

ViewFlipper结合手势OnGestureListener制作的滑动切换效果 http://gundumw100.iteye.com/blog/905033




完整源代码:http://download.csdn.net/detail/sxsj333/3615788

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值