[英]A simple one touch touch Drawing View but keep getting ACTION_CANCEL before touches completed
This has been driving me crazy. 这让我发疯。
I think I have everything in place but no matter what I do the touches seem to be cancelled before I lift my finger off the view. 我想我已经准备好一切,但是无论我做什么,在我将手指移开视线之前,触摸似乎都被取消了。 Even stranger I can draw long horizontal lines but vertical ones are always very short. 即使是陌生人,我也可以画长的水平线,但是垂直的线总是很短。
I am using a Samsung G SII 2.3.3 but building to 2.1 我正在使用Samsung G SII 2.3.3,但要升级到2.1
Ant ideas? 蚂蚁的想法?
My sample code: 我的示例代码:
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);
}
}
}
UPDATE: OK, I figured this out. 更新:好的,我知道了。 Because this view is inside another View some of the touches are being intercepted by the parent or parents. 由于此视图在另一个视图中,因此某些触摸被父级或多级父级拦截。
The solution I have found to be good enough for my needs is to add the following line into the case for ACTION_DOWN : 我发现足以满足我的需求的解决方案是在ACTION_DOWN的情况下添加以下行:
getParent().requestDisallowInterceptTouchEvent(true);
This then allows my view to get all touches. 然后,这将使我的视图获得所有效果。
The solution I have found to be good enough for my needs is to add the following line into the case for ACTION_DOWN : 我发现足以满足我的需求的解决方案是在ACTION_DOWN的情况下添加以下行:
getParent().requestDisallowInterceptTouchEvent(true);
This then allows my view to get all touches. 然后,这将使我的视图获得所有效果。
I edited my answer... 我修改了答案...
I found a finger-painting example in the Android Samples, and also a discussion about that example on stackoverflow. 我在Android示例中找到了一个手绘图示例,并且还在stackoverflow上找到了关于该示例的讨论。 Hope this helps you out. 希望这可以帮助你。
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html 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? 使用Canvas的Android FingerPaint示例,离屏Canvas是什么?
My original post... 我的原始帖子...
I don't have a solution, but did find a site that might give you some ideas on what you could do... 我没有解决方案,但确实找到了一个网站,该网站可能会给您一些有关您可以做什么的想法...
http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/ http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.