【问题标题】:A simple one touch touch Drawing View but keep getting ACTION_CANCEL before touches completed一个简单的一键式绘图视图,但在触摸完成之前不断收到 ACTION_CANCEL
【发布时间】:2011-11-15 20:16:51
【问题描述】:

这让我发疯了。

我想我已经准备好了一切,但无论我做什么,在我将手指从视野中抬起之前,触摸似乎都被取消了。更奇怪的是,我可以画很长的水平线,但垂直线总是很短。

我使用的是三星 G SII 2.3.3,但正在构建到 2.1

蚂蚁的想法?

我的示例代码:

  package com.mycompany.myviews;

  import java.util.ArrayList;

  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.graphics.Point;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.View;

  public class CustomView extends View
  {
    private ArrayList<DrawPoint> points = new ArrayList<DrawPoint>();

    public CustomView(Context context, AttributeSet attrs)
    {
      super(context, attrs);
    }

    public void addPoint(DrawPoint p)
    {
      points.add(p);
    }

      public boolean onTouchEvent (MotionEvent event)
      {
        DrawPoint p = new DrawPoint((int)event.getX(), (int)event.getY());

        switch(event.getAction())
        {
        case android.view.MotionEvent.ACTION_DOWN:
          p.start = true;
          break;

        case android.view.MotionEvent.ACTION_CANCEL:
          Log.d("TouchView", "On Touch cancelled.");
          break;
        }

        addPoint(p);
        invalidate();

        return true;
      }

    public void onDraw(Canvas c) 
    {
        super.onDraw(c);

        Path path = new Path();

        for (int i = 0; i < points.size(); i++)
        {
          DrawPoint currentPoint = points.get(i);
          if (currentPoint.start == true)
            path.moveTo(currentPoint.p.x, currentPoint.p.y);
          else
            path.lineTo(currentPoint.p.x, currentPoint.p.y);
        }

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.BLACK);
        c.drawPath(path, paint);
    }

    private class DrawPoint
    {
      public boolean start = false;
      public Point p;

      DrawPoint(int x, int y)
      {
        p = new Point(x, y);
      }
    }
  }

更新:好的,我想通了。因为这个视图在另一个视图中,所以一些触摸被父级或父级拦截。

我发现足以满足我需求的解决方案是将以下行添加到 ACTION_DOWN 的案例中:

getParent().requestDisallowInterceptTouchEvent(true);

这样就可以让我的视图得到所有的接触。

【问题讨论】:

    标签: android canvas touch-event ondraw


    【解决方案1】:

    我编辑了我的答案...

    我在 Android 示例中找到了一个手指绘画示例,并且还在 stackoverflow 上找到了关于该示例的讨论。希望对您有所帮助。

    http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

    Android FingerPaint Example using Canvas, what is the offscreen Canvas?

    我原来的帖子...

    我没有解决方案,但确实找到了一个可以为您提供一些解决方案的网站 关于你能做什么的想法......

    http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/

    【讨论】:

    • 谢谢,它确实展示了如何使用 OnDraw,但在触摸方面我看不到任何东西。
    【解决方案2】:

    我发现足以满足我需求的解决方案是将以下行添加到 ACTION_DOWN 的案例中:

    getParent().requestDisallowInterceptTouchEvent(true);
    

    这样就可以让我的视图得到所有的接触。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多