我在 Canvas 上绘制了一个 shapedrawables,我想在触摸事件中检测哪些形状被触摸了。 无需通过 (x,y) 计算,就像存在于 html 中一样自动.. 谢谢! :)
我成功地绘制了形状,并阅读了很多关于如何通过使用 (x,y) 计算来检测触摸的内容,但是如果我有可能自动检测触摸的形状,那么这样做并不聪明。 如果我不能用 Canvas 做到这一点,我将很高兴听到如何使用 android 中的其他小部件绘制形状和检测触摸的形状。
public class CanvasView extends View implements View.OnTouchListener
{
private List<ShapeDrawable> shapes;
private ShapeDrawable currentCircle;
public CanvasViewenter code here(Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
this.shapes = new ArrayList<>();
}
@Override
protected void onDraw(final Canvas canvas)
{
super.onDraw(canvas);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
//here I want to get all the shapes that have been touched
List<ShapeDrawable> touchedShapes = null;
return true;
}
}
最佳答案
根据 Canvas 的实际情况,我认为它没有任何方法可以从本质上知道绘制在其上的形状的位置。
跟踪绘制形状的位置(不幸的是使用坐标),并针对触摸事件进行检查可能是唯一的选择。
我说“可能”,因为我不是这方面的专家,但我过去经常使用 Canvas 。
也就是说,有一些库可以简化这一点。在我开发的一个 map 应用程序中,我使用了这个:
https://github.com/onlylemi/MapView
此库内置支持了解事物(形状)在 map 上的位置。也就是说,这个库在下面使用坐标和触摸事件(如在他们的 MarkLayer.java 类中所见),就像您会发现的大多数一样。
关于android - 如何检测在 Canvas 上绘制的 shapedrawable 是否被触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493050/