event.getAction() == MotionEvent.ACTION_OUTSIDE 不起作用
时间: 2023-12-14 09:38:29 浏览: 22
`MotionEvent.ACTION_OUTSIDE` 表示用户在当前 View 之外触摸屏幕,这种事件通常用于取消当前操作或隐藏弹出窗口。但是,如果你想要在 View 之外进行一些操作,比如隐藏当前 View,你可以实现 View 的 onTouchEvent 方法,判断 MotionEvent 的动作类型是否为 `ACTION_OUTSIDE`,然后在适当的时候执行相关操作。
如果你遇到了 `MotionEvent.ACTION_OUTSIDE` 不起作用的情况,可能是因为你的 View 并没有设置可点击或可触摸。你可以在 View 的构造函数或 `onCreate` 方法中添加如下代码来启用触摸事件:
```
setClickable(true);
setFocusable(true);
setFocusableInTouchMode(true);
```
如果你的 View 是一个自定义的 ViewGroup,你需要在其子 View 的 `onTouchEvent` 方法中调用 `super.onTouchEvent(event)` 来保证父 View 的 `onTouchEvent` 方法能够被调用。同时,在子 View 中的 `onTouchEvent` 方法中也需要判断 `ACTION_OUTSIDE` 事件并执行相应的操作。
相关问题
case MotionEvent.ACTION_DOWN:
当触摸事件为 `MotionEvent.ACTION_DOWN` 时,表示用户按下了手指。在这种情况下,你可以执行相应的操作来响应按下事件。
在你提供的代码片段中,当触摸事件为 `MotionEvent.ACTION_DOWN` 时,执行以下操作:
1. 获取当前触摸点的横坐标和纵坐标:
```java
x = (int) event.getRawX();
y = (int) event.getRawY();
```
这里使用 `event.getRawX()` 和 `event.getRawY()` 方法来获取触摸点在屏幕上的绝对坐标。
2. 可以根据需要对获取到的坐标进行处理,例如更新UI界面、记录触摸起始点等。
根据你的实际需求,你可以在 `MotionEvent.ACTION_DOWN` 分支中添加相应的代码来处理按下事件。例如,你可以在按下事件中执行拖动操作、显示一个提示框等等。
下段代码 可以拖动,但缩放和旋转时会闪退,请改正 public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // 手指压下屏幕 mode = MODE.DRAG; // 查找被点击的图片 int index = event.getActionIndex(); float x = event.getX(index); float y = event.getY(index); CustomBitmap clickedBitmap = findClickedBitmap(x, y); if (clickedBitmap != null) { // 切换操作对象 _bitmaps.remove(clickedBitmap); _bitmaps.add(clickedBitmap); // 记录ImageView当前的移动位置 currentMatrix.set(clickedBitmap.matrix); clickedBitmap.matrix.set(currentMatrix); clickedBitmap.startPoint.set(x, y); _curCustomBitmap = clickedBitmap; } postInvalidate(); break; case MotionEvent.ACTION_POINTER_DOWN: // 当屏幕上还有触点(手指),再有一个手指压下屏幕 mode = MODE.ZOOM; // 记录位图的旋转角度和缩放倍数 _curCustomBitmap.oldRotation = rotation(event); _curCustomBitmap.startDis = distance(event); if (_curCustomBitmap.startDis > 10f) { // 获取缩放中心点的坐标 float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); _curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2); // 记录ImageView当前的缩放倍数 currentMatrix.set(_curCustomBitmap.matrix); } break; case MotionEvent.ACTION_MOVE: // 手指在屏幕移动,该事件会不断地触发 if (mode == MODE.DRAG) { // 移动图片 float dx = event.getX() - _curCustomBitmap.startPoint.x; float dy = event.getY() - _curCustomBitmap.startPoint.y; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postTranslate(dx, dy); } else if (mode == MODE.ZOOM) { // 缩放和旋转图片 float endDis = distance(event); float rotation = rotation(event) - _curCustomBitmap.oldRotation; if (endDis > 10f) { float scale = endDis / _curCustomBitmap.startDis; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); _curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); } } break; case MotionEvent.ACTION_POINTER_UP: // 有手指离开屏幕,但屏幕还有触点(手指) mode = MODE.NONE; break; } invalidate(); return true;
可能是因为在缩放和旋转时,没有判断当前操作的图片是否为null,导致空指针异常。可以在缩放和旋转的代码之前添加判断,如果当前操作的图片为空,就直接返回。修改后的代码如下:
```
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// 手指压下屏幕
mode = MODE.DRAG;
// 查找被点击的图片
int index = event.getActionIndex();
float x = event.getX(index);
float y = event.getY(index);
CustomBitmap clickedBitmap = findClickedBitmap(x, y);
if (clickedBitmap != null) {
// 切换操作对象
_bitmaps.remove(clickedBitmap);
_bitmaps.add(clickedBitmap);
// 记录ImageView当前的移动位置
currentMatrix.set(clickedBitmap.matrix);
clickedBitmap.matrix.set(currentMatrix);
clickedBitmap.startPoint.set(x, y);
_curCustomBitmap = clickedBitmap;
}
postInvalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN:
// 当屏幕上还有触点(手指),再有一个手指压下屏幕
mode = MODE.ZOOM;
// 记录位图的旋转角度和缩放倍数
if (_curCustomBitmap == null) {
return true;
}
_curCustomBitmap.oldRotation = rotation(event);
_curCustomBitmap.startDis = distance(event);
if (_curCustomBitmap.startDis > 10f) {
// 获取缩放中心点的坐标
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
_curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2);
// 记录ImageView当前的缩放倍数
currentMatrix.set(_curCustomBitmap.matrix);
}
break;
case MotionEvent.ACTION_MOVE:
// 手指在屏幕移动,该事件会不断地触发
if (mode == MODE.DRAG) {
// 移动图片
if (_curCustomBitmap == null) {
return true;
}
float dx = event.getX() - _curCustomBitmap.startPoint.x;
float dy = event.getY() - _curCustomBitmap.startPoint.y;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postTranslate(dx, dy);
} else if (mode == MODE.ZOOM) {
// 缩放和旋转图片
if (_curCustomBitmap == null) {
return true;
}
float endDis = distance(event);
float rotation = rotation(event) - _curCustomBitmap.oldRotation;
if (endDis > 10f) {
float scale = endDis / _curCustomBitmap.startDis;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
_curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
}
}
break;
case MotionEvent.ACTION_POINTER_UP:
// 有手指离开屏幕,但屏幕还有触点(手指)
mode = MODE.NONE;
break;
}
invalidate();
return true;
}
```