您的位置:首页 > 移动开发 > Android开发

Android左右滑动切换背景(GestureDetector)

2014-03-24 16:51 411 查看
本文利用OnGestureListener,  OnTouchListener这两个接口来实现一个左右切换背景图片的demo
其中OnTouchListener用于获取用户对手机的操作,如触摸、拖动等

实现方式比较简单,但是实现效果也比较粗糙
 
学习内容来源于以下两篇文章:
http://wayfarer.iteye.com/blog/460284
http://blog.csdn.net/zqiang_55/article/details/8009127

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnGestureListener,OnTouchListener{

int flag =0;
LinearLayout layout = null;
GestureDetector gd = null;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gd = new GestureDetector(this);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);

layout.setBackgroundResource(R.drawable.page1);
layout.setOnTouchListener(this);
layout.setLongClickable(true);

}

/*
* 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
* 来分析是否有合适的callback函数来处理用户的手势
*/
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return this.gd.onTouchEvent(event);
}

// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {

// 参数解释:
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒

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

if (e1.getX() - e2.getX() > 100) {
if(flag == 0){
layout.setBackgroundResource(R.drawable.page1);
flag++;
return true;
}
if(flag == 1){
layout.setBackgroundResource(R.drawable.page2);
flag++;
return true;
}
if(flag == 2){
layout.setBackgroundResource(R.drawable.page3);
flag = 0;
return true;
}
}else if (e1.getX() - e2.getX() < -100) {
if(flag == 0){
layout.setBackgroundResource(R.drawable.page1);
flag = 2;
return true;
}
if(flag == 1){
layout.setBackgroundResource(R.drawable.page2);
flag--;
return true;
}
if(flag == 2){
layout.setBackgroundResource(R.drawable.page3);
flag--;
return true;
}
}
return false;
}

// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

/*
* 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
* 注意和onDown()的区别,强调的是没有松开或者拖动的状态
*/
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息