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;
}
}
其中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;
}
}
相关文章推荐
- Android GestureDetector 左右滑动 切换 ViewFlipper的子View
- Android_左右滑动切换背景
- Android_左右滑动切换背景
- Android_左右滑动切换背景
- android GestureDetector简单手势检测(左右滑动、上下滑动)
- 【Android开发日记】左右滑动刷新界面的思路记录 ViewPager GestureDetector
- Android_左右滑动切换背景
- 实现了在android实现左右滑动切换界面的效果
- Android的Activity屏幕切换动画左右滑动切换
- Android的Activity屏幕切换动画-左右滑动切换
- 学习GestureDetectorCompat,实现卡片左右滑动消失效果
- Android中实现整个视图切换的左右滑动效果
- Android的Activity屏幕切换动画(一)-左右滑动切换
- Android 实现多页界面左右滑动切换效果之进阶
- Android的Activity屏幕切换动画-左右滑动深入与实战
- Android开发实现高仿优酷的客户端图片左右滑动切换功能实例【附源码下载】
- 高仿优酷Android客户端图片左右滑动(自动切换)
- Android客户端图片左右滑动(自动切换)
- android之左右滑动切换activity
- Android的Activity屏幕切换动画(二)-左右滑动深入与实战