触摸时 ACTION_CANCEL [英] ACTION_CANCEL while touching

查看:24
本文介绍了触摸时 ACTION_CANCEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类表示可触摸的视图并绘制滑动条.

I has the following class that represents a View that is touchable and draw a Slide Bar.

public class SlideBar extends View {
private int progress;
private int max;

private Paint background;
private Paint upground;

private RectF bar;

private boolean firstDraw;

public SlideBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    progress = 0;

    upground = new Paint();
    upground.setColor(Color.parseColor("#C2296C"));

    background = new Paint();
    background.setColor(Color.parseColor("#777777"));
}

private void onFirstDraw() {
    max = getWidth();
    bar = new RectF(0, 19, max, 21);
}

public void onDraw(Canvas canvas) {
    if (!firstDraw) {
        onFirstDraw();
        progress = max;
        firstDraw = true;
    }

    canvas.save();
    canvas.drawRoundRect(bar, 5, 5, background);
    canvas.drawCircle(progress, 20, 9, upground);
    canvas.restore();
}

public void setValue(int value) {
    progress = value;
}

public boolean onTouchEvent(MotionEvent evt) {
    System.out.println(evt.getAction());
    progress = (int) evt.getX();
    invalidate();
    return false;
}
}

但是当触摸和拖动它时,我收到一个 ACTION_DOWN,一些 ACTION_MOVE 然后收到一个 ACTION_CANCEL 并且没有进一步的事件.

But when touching and dragging it, I receive a ACTION_DOWN, some ACTION_MOVEs then receive a ACTION_CANCEL and no further events.

为什么会这样?我不想取消事件并启用它继续拖动栏.

Why it's happens? I don't want to cancel the event and enable it to keep dragging bar.

推荐答案

ACTION_CANCEL 发生在父视图接管其子视图之一的控制权时.

An ACTION_CANCEL happens when a parent view takes over control of one of its children views.

查看关于 ViewGroup.onInterceptTouchEvent(MotionEvent) 方法.来自链接:

Take a look at the documentation around ViewGroup.onInterceptTouchEvent(MotionEvent) method. From the link:

  1. 您将在此处收到 down 事件.
  2. down 事件要么由这个视图组的子视图处理,要么交给你自己的 onTouchEvent() 方法来处理;这意味着您应该实现 onTouchEvent() 以返回 true,因此您将继续看到手势的其余部分(而不是寻找父视图来处理它).此外,通过从 onTouchEvent() 返回 true,您将不会在 onInterceptTouchEvent() 中收到任何后续事件,并且所有触摸处理都必须在 onTouchEvent() 中发生代码>像往常一样.
  3. 只要您从此函数返回 false,接下来的每个事件(直到并包括最终事件)将首先传递到此处,然后传递到目标的 onTouchEvent().
  4. 如果您从这里返回 true,您将不会收到任何以下事件:目标视图将收到相同的事件,但带有操作 ACTION_CANCEL,并且所有进一步的事件都将传递给您的 onTouchEvent() 方法,这里不再出现
  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here

这篇关于触摸时 ACTION_CANCEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆