【问题标题】:How to detect shapedrawable that drawn on a canvas was touched?如何检测在画布上绘制的 shapedrawable 是否被触摸?
【发布时间】:2019-02-02 12:24:43
【问题描述】:

我在画布上绘制了一个 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;
    }
}

【问题讨论】:

    标签: android android-canvas shapes ontouch shapedrawable


    【解决方案1】:

    根据画布的实际情况,我不认为它有任何方法可以从本质上知道绘制在其上的形状的位置。

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

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

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

    https://github.com/onlylemi/MapView

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多