使画布的线条可点击

3
根据这个答案,我创建了一个 rectF 的 arrayList。 如何使canvas的drawLine()可点击? 以下是代码逻辑:
List<RectF> rectFs;

Point pt1;
Point pt2;

那么

path.moveTo(pt1.x, pt1.y);
path.lineTo(pt2.x, pt2.y);

path.computeBounds(rectF, true);
rectFs.add(rectF);

然后,我有一个方法来检查被点击和rectF数组列表。

void lineHighighted(Point pt) {
    int ct = 0;
    for(RectF rectF : rectFs) {
        if(rectF.contains(pt.x, pt.y)) {
            ct++;
            Log.d(tag, ct + "HERE");
        }
    }
}

我的问题是,有时候整个ArrayList被选中或“调用”,即使我没有触碰那一行代码。
我的代码有什么问题吗?
提前感谢。
补充:
我发现在我的画布中添加了这段代码后:
path.moveTo(coor1[0], coor1[1]);
path.lineTo(coor2[0], coor2[1]);
canvas.drawPath(path, paint2);
path.computeBounds(rectf, true);

我的先前结果: 添加代码之前

现在变成了这样: 输入图像说明

1个回答

0

这很有可能!因为我没有看到您与画布交互的代码,所以我将发布完全工作的代码。逻辑是一行不能覆盖另一行,以确保正确的功能。希望我能帮到您。

    // setting where I will draw the ImageView for taking pictures

    // rec is used for onInterceptTouchEvent. I pass this from the
    // highest to lowest layer so that when this area of the screen
    // is pressed, it ignores the TouchView events and passes it to
    // this activity so that the button can be pressed.
    rec.set((int)((double)mScreenWidth*.85),
            (int)((double)mScreenHeight*.10) ,
            (int)((double)mScreenWidth*.85)+mButtonDrawable.getMinimumWidth(), 
            (int)((double)mScreenHeight*.70)+mButtonDrawable.getMinimumHeight());
    mButtonDrawable = null;

根据这个逻辑,您可以在此逻辑中使用任何您想要的内容,它使单击操作不被覆盖。


谢谢您的回答,先生。我非常感激。 如果可以的话,您能否帮我检查一下我在顶部提出的附加问题吗?提前致谢 :) - MaChee Neraid
抱歉,我无法加载您的图片,您可以重新上传吗?谢谢:) - BiggDawgg
我找到了问题所在:似乎是path.computebounds(rectf, true)这个方法有问题。你遇到过这种情况吗? - MaChee Neraid
哦不,我从来没有遇到过那个问题。 - BiggDawgg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接