2

我在画布上绘制了一个 shapedrawables,我想在触摸事件中检测触摸了哪些形状。无需按 (x,y) 计算,就像 html 中存在的那样自动计算。谢谢!:)

我成功地绘制了形状,并阅读了很多关于如何通过使用 (x,y) 计算来检测 ontouch 的内容,但如果我有可能自动检测到触摸的形状,那么这样做并不聪明。如果我不能用画布做到这一点,我会很高兴听到我如何绘制形状并使用 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;
    }
}
4

1 回答 1

0

基于画布实际上是什么,我不相信它有任何方法可以从本质上知道绘制在它上面的形状的位置。

跟踪绘制形状的位置(不幸的是使用坐标),并检查触摸事件可能是唯一的选择。

我说“可能”,因为我不是这方面的专家,但我过去经常使用画布。

也就是说,有一些库可以简化这一点。在我工作的一个地图应用程序中,我使用了这个:

https://github.com/onlylemi/MapView

该库内置支持了解事物(形状)在地图上的位置。也就是说,这个库在下面使用坐标和触摸事件(如在他们的 MarkLayer.java 类中所见),您会发现大多数情况。

于 2019-02-02T13:01:07.410 回答