RT,我参照了下面这个网上找的帖子,但是在实现中遇到问题,能不能帮我具体实现一下ACTION_DOWN、ACTION_MOVE、ACTION_UP中的内容???
Android提供了GestureDetector类来处理一些常用的手势操作,比如说 onLongPress,onFling 等。但这里不使用GestureDetector,而是直接在自定义View重写的onTouchEvent中进行处理。欲实现的效果是:当手机按住屏幕时,如果在指定的时间内没有移动(如500毫秒),那么进入长按模式,此时手指在屏幕上移动都算作长按模式。如果手机按住屏幕就立马移动,那么就算作移动模式。MotionEvent 类提供了记录当前坐标的函数(getX(),getY())和当前事件产生的时间的函数(getEventTime())以及按下时间(getDowntime())。MotionEvent同时也提供了当前的操作类型,按下(ACTION_DOWN)、 移动 (ACTION_MOVE)、弹起 (ACTION_UP)。有了这些参数,我们便可以轻易的实现想要的效果了。大概思路如下:在按下时记录x,y坐标以及按下时间,当第一次移动的时候获取移动的时间,如果大于指定的长按时间,那么进入长按模式,否则就是普通的移动模式。很容易,在模拟器里面实现了这个效果,但是当在真机里面运行时,却无法实现这样的效果。原来模拟器点击的时候能够保证在不移动鼠标的情况下不触发ACTION_MOVE,但是真机却很敏感,几乎在ACTION_DOWN后的几毫秒之后就立马不停的ACTION_MOVE了。想了一下,其实只要稍微变通下变可以在真机上也实现相同的效果了。那就是判断ACTION_MOVE后的坐标和ACTION_DOWN的坐标的偏移值是否小于我们指定的偏移像素,如果在指定值内,那么认为没有移动。于是有了如下这个函数。/**
* 判断是否有长按动作发生
* @param lastX 按下时X坐标
* @param lastY 按下时Y坐标
* @param thisX 移动时X坐标
* @param thisY 移动时Y坐标
* @param lastDownTime 按下时间
* @param thisEventTime 移动时间
* @param longPressTime 判断长按时间的阀值
*/
private boolean isLongPressed(float lastX,float lastY,
float thisX,float thisY,
long lastDownTime,long thisEventTime,
long longPressTime){
float offsetX = Math.abs(thisX - lastX);
float offsetY = Math.abs(thisY - lastY);
long intervalTime = thisEventTime - lastDownTime;
if(offsetX <=10 && offsetY<=10 && intervalTime >= longPressTime){
return true;
}
return false;
}
在ACTION_DOWN的时候,记录下lastX,lastY和lastDownTime,在ACTION_MOVE的时候判断当前是否为长按模式(类标志变量的方式),如果不是,那么获取当前的thisX,thisY和thisEventTime调用函数进行判断。最后别忘记在ACTION_UP里将长按标志值为FALSE。ACTION_DOWN里面这样处理://检测是否长按,在非长按时检测
if(!mIsLongPressed){
mIsLongPressed = isLongPressed(mLastMotionX, mLastMotionY, x, y, lastDownTime,eventTime,500);
}
if(mIsLongPressed){
//长按模式所做的事
}else{
//移动模式所做的事
}
Android提供了GestureDetector类来处理一些常用的手势操作,比如说 onLongPress,onFling 等。但这里不使用GestureDetector,而是直接在自定义View重写的onTouchEvent中进行处理。欲实现的效果是:当手机按住屏幕时,如果在指定的时间内没有移动(如500毫秒),那么进入长按模式,此时手指在屏幕上移动都算作长按模式。如果手机按住屏幕就立马移动,那么就算作移动模式。MotionEvent 类提供了记录当前坐标的函数(getX(),getY())和当前事件产生的时间的函数(getEventTime())以及按下时间(getDowntime())。MotionEvent同时也提供了当前的操作类型,按下(ACTION_DOWN)、 移动 (ACTION_MOVE)、弹起 (ACTION_UP)。有了这些参数,我们便可以轻易的实现想要的效果了。大概思路如下:在按下时记录x,y坐标以及按下时间,当第一次移动的时候获取移动的时间,如果大于指定的长按时间,那么进入长按模式,否则就是普通的移动模式。很容易,在模拟器里面实现了这个效果,但是当在真机里面运行时,却无法实现这样的效果。原来模拟器点击的时候能够保证在不移动鼠标的情况下不触发ACTION_MOVE,但是真机却很敏感,几乎在ACTION_DOWN后的几毫秒之后就立马不停的ACTION_MOVE了。想了一下,其实只要稍微变通下变可以在真机上也实现相同的效果了。那就是判断ACTION_MOVE后的坐标和ACTION_DOWN的坐标的偏移值是否小于我们指定的偏移像素,如果在指定值内,那么认为没有移动。于是有了如下这个函数。/**
* 判断是否有长按动作发生
* @param lastX 按下时X坐标
* @param lastY 按下时Y坐标
* @param thisX 移动时X坐标
* @param thisY 移动时Y坐标
* @param lastDownTime 按下时间
* @param thisEventTime 移动时间
* @param longPressTime 判断长按时间的阀值
*/
private boolean isLongPressed(float lastX,float lastY,
float thisX,float thisY,
long lastDownTime,long thisEventTime,
long longPressTime){
float offsetX = Math.abs(thisX - lastX);
float offsetY = Math.abs(thisY - lastY);
long intervalTime = thisEventTime - lastDownTime;
if(offsetX <=10 && offsetY<=10 && intervalTime >= longPressTime){
return true;
}
return false;
}
在ACTION_DOWN的时候,记录下lastX,lastY和lastDownTime,在ACTION_MOVE的时候判断当前是否为长按模式(类标志变量的方式),如果不是,那么获取当前的thisX,thisY和thisEventTime调用函数进行判断。最后别忘记在ACTION_UP里将长按标志值为FALSE。ACTION_DOWN里面这样处理://检测是否长按,在非长按时检测
if(!mIsLongPressed){
mIsLongPressed = isLongPressed(mLastMotionX, mLastMotionY, x, y, lastDownTime,eventTime,500);
}
if(mIsLongPressed){
//长按模式所做的事
}else{
//移动模式所做的事
}
解决方案 »
- 请教:使用NDK,是否可以调用Linux系统调用
- 求助:gbk转unicode的问题
- adobe android pdf
- android 上面如何删除虚拟的键盘
- 求助!怎么能判断Gallery的滚动已经停止了???
- <Android>XMPP+openfire+smack 移动端开发创建多人聊天遇到的问题!
- 求助: 有用过科大讯飞语音的吗?帮忙看给看一下,不能运行?
- 全志 android4.2 内核编译命令
- 开发手机网站需要用什么技术?
- 在Android中导入httpcore-4.3.2.jar包,调用EntityUtils.consume(HttpEntity)提示找不到该方法
- onclicklistener问题(新手问题)
- Android 的Application( 不同的类中进行数据的操作)疑问
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;public class MyTextView extends View {
boolean isLongClickModule = false; public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public MyTextView(Context context) {
super(context);
init();
}
private void init() {
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
System.out.println("onLongClick");
isLongClickModule = true;
return false;
}
});
} @Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
if (isLongClickModule) {
System.out.println("ACTION_MOVE->LongClickModule");
} else {
System.out.println("ACTION_MOVE->NotLongClickModule");
}
break;
case MotionEvent.ACTION_UP:
isLongClickModule = false;
System.out.println("ACTION_UP");
break;
}
return super.onTouchEvent(ev);
}}
修改后如下:
package com.example.android_test;import java.util.Timer;
import java.util.TimerTask;import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;public class MyTextView extends View {
boolean isLongClickModule = false;
float startX,startY;
Timer timer; public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyTextView(Context context) {
super(context);
} @Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("ACTION_DOWN");
startX = ev.getX();
startY = ev.getY();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
isLongClickModule = true;
}
}, 500);
break;
case MotionEvent.ACTION_MOVE:
double deltaX = Math.sqrt((ev.getX() - startX) * (ev.getX() - startX) + (ev.getY() - startY) * (ev.getY() - startY));
if (deltaX > 10 && timer != null) { // 移动大于10像素
timer.cancel();
timer = null;
}
System.out.println("ACTION_MOVE" + (isLongClickModule == true ? "长按" : "非长按"));
break;
case MotionEvent.ACTION_UP:
isLongClickModule = false;
System.out.println("ACTION_UP");
break;
}
return true;
}}