技术,使画布的drawLine()点击?画布、技术、drawLine

2023-09-07 23:31:41 作者:抹不去的曾经

我工作的一个应用程序,绘制地图上的节点,每个节点有重新通过它们之间的直线psented $ P $边缘。我画用帆布的drawLine()的边缘,但如果自己的线路可以点击这将是有益的。我的意思是允许用户触摸线的方法,或认为他们在接触线和事件可以触发。 (如显示屏边缘信息,等...)

I'm working on an app that plots nodes on a map, and each node has edges that are represented by a line between them. I've drawn the edges using Canvas and drawLine(), but it would be useful if the lines themselves could be clickable. By that I mean a method of allowing the user to touch the line or think they're touching the line and an event can trigger. (like display edge info, etc...)

我无法正确地附加一个触摸事件到行我画与油画,所以我想将ImageViews插图中的每个边缘线是被画的两端。 ImageView的可能是一个点所以很清楚那里的触摸事件触发。

I can't rightly attach a touch event to a line I've drawn with Canvas, so I was thinking of placing ImageViews inbetween the ends of each edge line that's drawn. The ImageView could be a dot so it's clear where the touch event triggers.

有没有人有任何其他建议?我主要是在寻找,我已经错过了想法。也许有一些Android的API中,可以帮助这一点,我不知道。

Does anyone have any other suggestions? I'm mainly looking for ideas that I've missed. Maybe there's something in the Android API that can help with this that I'm unaware of.

在此先感谢任何提示!

推荐答案

使用路径来绘制行:

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);

覆盖的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;
}