Technique to make a canvas drawLine() clickable?

大城市里の小女人 提交于 2019-11-29 16:44:30

Use a path to draw the line:

Path linePath;
Paint p;
RectF rectF;
float point1X, point1Y, point2X, point2Y;

// initialize components

// draw the line
linePath.moveTo(point1X, point1Y); 
linePath.lineTo(point2X, point2Y);

canvas.drawPath(linePath, p);

linePath.computeBounds(rectF, true);

Override onTouchEvent(MotionEvent):

@Override
public boolean onTouchEvent(MotionEvent event) {

    float touchX = event.getX();
    float touchY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (rectF.contains(touchX, touchY)) {
            // line has been clicked
        }
        break;
    }
    return true;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!