3点击后,OnTouchListener执行绘制

时间:2013-10-24 13:54:52

标签: android ontouchevent

我有自定义视图,并在该customView类中重写onTouchEvent。我想在点击customView时显示三个弧。我已经实现了以下代码来处理该视图上的点击事件。

@Override
public boolean onTouchEvent(final MotionEvent event) {

  if (event.getActionMasked() == MotionEvent.ACTION_UP) {
    Paint p = new Paint();
    RectF rectF = new RectF(50, 50, 500, 500);
    p.setColor(Color.BLACK);
    canvas.drawArc(rectF, 0, 45, true, p);
    canvas.drawArc(rectF, 145, 45, true, p);
    canvas.drawArc(rectF, 260, 45, true, p);
    invalidate(50, 50, 500, 500);
    return performClick();
  }
  return true;
}

请说明我做了什么错误。

1 个答案:

答案 0 :(得分:0)

为什么在点击完成后需要调用performClick()? 如果处理了事件,则应返回true,否则返回false。所以我认为核心变体应该是

@Override
public boolean onTouchEvent(final MotionEvent event) {

  if (event.getActionMasked() == MotionEvent.ACTION_UP) {
    Paint p = new Paint();
    RectF rectF = new RectF(50, 50, 500, 500);
    p.setColor(Color.BLACK);
    canvas.drawArc(rectF, 0, 45, true, p);
    canvas.drawArc(rectF, 145, 45, true, p);
    canvas.drawArc(rectF, 260, 45, true, p);
    invalidate(50, 50, 500, 500);
    return true;
  }
  return false;
}