运行有问题或需要源码请点赞关注收藏后评论区留言~~~
dispatchTouchEvent onInterceptTouchEvent onTouchEvent三个方法的输入参数都是手势事件MotionEvent,其中包含触摸动作的所有信息,各种手势操作都从MotionEvent中获取触摸信息并判断处理。 下面是MotionEvent的常用方法
getAction 获取当前的动作类型 取值如下
ACTION_DOWN 按下动作
ACTION_UP 提起动作
ACTION_MOVE 移动动作
ACTION_CANCEL 取消动作
ACTION_OUTSIDE 移出边界动作
ACTION_POINTER_DOWN 第二个点的按下动作 用于多点触控的判断
ACTION_POINTER_UP 第二个点的提起动作
ACTION_MASK 动作掩码 获得多点触控信息
getEventTime 获取事件时间
getX 获取在控件内部的相对横坐标
getY 获取在控件内部的相对纵坐标
getRawX 获取在屏幕上的绝对横坐标
getRawY 获取在屏幕上的绝对纵坐标
getPressure 获取触摸的压力大小
getPointerCount 获取触控点的数量
单点触摸效果如下
点击后可以显示一系列点击信息 当然最好连接真机测试 效果更好

代码如下
package com.example.event;import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.TextView;@SuppressLint("DefaultLocale")
public class TouchSingleActivity extends AppCompatActivity {private TextView tv_touch; // 声明一个文本视图对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_touch_single);tv_touch = findViewById(R.id.tv_touch);}// 在发生触摸事件时触发@Overridepublic boolean onTouchEvent(MotionEvent event) {// 从开机到现在的毫秒数int seconds = (int) (event.getEventTime() / 1000);String desc = String.format("动作发生时间:开机距离现在%02d:%02d:%02d",seconds / 3600, seconds % 3600 / 60, seconds % 60);desc = String.format("%s\n动作名称是:", desc);int action = event.getAction(); // 获得触摸事件的动作类型if (action == MotionEvent.ACTION_DOWN) { // 按下手指desc = String.format("%s按下", desc);} else if (action == MotionEvent.ACTION_MOVE) { // 移动手指desc = String.format("%s移动", desc);} else if (action == MotionEvent.ACTION_UP) { // 松开手指desc = String.format("%s提起", desc);} else if (action == MotionEvent.ACTION_CANCEL) { // 取消手势desc = String.format("%s取消", desc);}desc = String.format("%s\n动作发生位置是:横坐标%f,纵坐标%f,压力为%f",desc, event.getX(), event.getY(), event.getPressure());tv_touch.setText(desc);return super.onTouchEvent(event);}}
除了单点触摸 智能手机还普遍支持多点触控,即响应两个及以上手指同时按压屏幕,多点触控可用于操纵图像的缩放与旋转操作以及需要多点处理的游戏界面
效果如下 分为主要动作和次要动作

代码如下
package com.example.event;import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.widget.TextView;@SuppressLint("DefaultLocale")
public class TouchMultipleActivity extends AppCompatActivity {private TextView tv_touch_major; // 声明一个文本视图对象private TextView tv_touch_minor; // 声明一个文本视图对象private boolean isMinorDown = false; // 次要点是否按下@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_touch_multiple);tv_touch_major = findViewById(R.id.tv_touch_major);tv_touch_minor = findViewById(R.id.tv_touch_minor);}// 在发生触摸事件时触发@Overridepublic boolean onTouchEvent(MotionEvent event) {// 从开机到现在的毫秒数int seconds = (int) (event.getEventTime() / 1000);String desc_major = String.format("主要动作发生时间:开机距离现在%02d:%02d:%02d\n%s",seconds / 3600, seconds % 3600 / 60, seconds % 60, "主要动作名称是:");String desc_minor = "";isMinorDown = (event.getPointerCount() >= 2);// 获得包括次要点在内的触摸行为int action = event.getAction() & MotionEvent.ACTION_MASK;if (action == MotionEvent.ACTION_DOWN) { // 按下手指desc_major = String.format("%s按下", desc_major);} else if (action == MotionEvent.ACTION_MOVE) { // 移动手指desc_major = String.format("%s移动", desc_major);if (isMinorDown) {desc_minor = String.format("%s次要动作名称是:移动", desc_minor);}} else if (action == MotionEvent.ACTION_UP) { // 松开手指desc_major = String.format("%s提起", desc_major);} else if (action == MotionEvent.ACTION_CANCEL) { // 取消手势desc_major = String.format("%s取消", desc_major);} else if (action == MotionEvent.ACTION_POINTER_DOWN) { // 次要点按下desc_minor = String.format("%s次要动作名称是:按下", desc_minor);} else if (action == MotionEvent.ACTION_POINTER_UP) { // 次要点松开desc_minor = String.format("%s次要动作名称是:提起", desc_minor);}desc_major = String.format("%s\n主要动作发生位置是:横坐标%f,纵坐标%f",desc_major, event.getX(), event.getY());tv_touch_major.setText(desc_major);if (isMinorDown || !TextUtils.isEmpty(desc_minor)) { // 存在次要点触摸desc_minor = String.format("%s\n次要动作发生位置是:横坐标%f,纵坐标%f",desc_minor, event.getX(1), event.getY(1));tv_touch_minor.setText(desc_minor);}return super.onTouchEvent(event);}}
创作不易 觉得有帮助请点赞关注收藏~~~
上一篇:一句话描述了一个男人长发