package com.example.cavasdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
public class MyView extends View {
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//单击,双击,或者滑动 最终都得到这三变量的值
private String actionType="action.type.none";//当前事件类型
private float actionX;//事件发生时的坐标
private float actionY;//事件发生时的坐标
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// TODO: 2023/1/11 做你想做的事情
}
private String ACTION_TYPE_ONCE_CLICK="action.type.once.click";//单击事件
private String ACTION_TYPE_DOUBLE_CLICK="action.type.double.click";//双击事件
private String ACTION_TYPE_MOVE="action.type.move";//滑动事件
private String ACTION_TYPE_NONE="action.type.none";//释放事件
private float downX;//按下时的坐标
private float downY;//按下时的坐标
private float timeInterval=500;//判断两次点击是不是双击的时间间隔
@Override
public boolean onTouchEvent(MotionEvent event) {
float X=event.getX();
float Y=event.getY();
if (event.getAction()==MotionEvent.ACTION_DOWN){
Log.d("fxHou","按下:X="+X+" Y="+Y);
judgeOnceClick =true;
downX=X;
downY=Y;
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE){
Log.d("fxHou","移动:X="+X+" Y="+Y);
judgeOnceClick =false;
actionX=X;
actionY=Y;
actionType=ACTION_TYPE_MOVE;
postInvalidate();
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
Log.d("fxHou","抬起:X="+X+" Y="+Y);
doubleClickJudge(event.getX(),event.getY());
return true;
}
return super.onTouchEvent(event);
}
//判断是不是双击
private long intervalTime =System.currentTimeMillis();
private float lastX=-1;
private float lastY=-1;
private void doubleClickJudge(float x, float y) {
boolean condition1=(System.currentTimeMillis() - intervalTime) < timeInterval;//两次点击时间间隔不超500毫秒
if (condition1) {
mHandle.removeMessages(KEY_ONCE_CLICK_TAG);
Log.d("fxHou","双击:X="+((lastX+x)/2f)+" Y="+((lastY+y)/2f));
actionX=((lastX+x)/2f);
actionY=((lastY+y)/2f);
actionType=ACTION_TYPE_DOUBLE_CLICK;
postInvalidate();
}else {
onceClickJudge(x,y);
}
lastX=x;
lastY=y;
intervalTime = System.currentTimeMillis();
}
//判断是不是单击
private boolean judgeOnceClick =false;//是否需要判断是不是单击,有过滑动就不用判断未单击
private final int KEY_ONCE_CLICK_TAG=0xFF;
private float onceX;
private float onceY;
private void onceClickJudge(float x, float y) {
if (judgeOnceClick){
onceX=x;
onceY=y;
Message message=new Message();
message.what=KEY_ONCE_CLICK_TAG;
mHandle.sendMessageDelayed(message, (long) timeInterval);
}
}
private android.os.Handler mHandle = new android.os.Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
if (msg.what==KEY_ONCE_CLICK_TAG){
Log.d("fxHou","单击:X="+onceX+" Y="+onceY);
actionX=onceX;
actionY=onceY;
actionType=ACTION_TYPE_ONCE_CLICK;
postInvalidate();
}
}
};
}
Android判断单击、双击、移动(自用)
最新推荐文章于 2024-08-23 16:35:20 发布
该代码示例展示了在Android中如何自定义一个View类(MyView),并重写onTouchEvent方法来处理单击、双击和滑动事件。通过ACTION_DOWN、ACTION_MOVE和ACTION_UP来判断用户操作类型,同时使用Handler和时间间隔判断单击和双击。
摘要由CSDN通过智能技术生成