根据这个答案,我创建了一个rectF的arrayList。
Technique to make a canvas drawLine() clickable?
这是我的代码逻辑:
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 arrayList。
void lineHighighted(Point pt) {
int ct = 0;
for(RectF rectF : rectFs) {
if(rectF.contains(pt.x, pt.y)) {
ct++;
Log.d(tag, ct + "HERE");
}
}
}
我的问题是,有时即使我没有触及那条“线”,也会选择或“调用”整个数组列表。
我的代码有什么问题吗?
提前致谢。
附加:
我发现在我的 Canvas 中添加这段代码后:
path.moveTo(coor1[0], coor1[1]);
path.lineTo(coor2[0], coor2[1]);
canvas.drawPath(path, paint2);
path.computeBounds(rectf, true);
我之前的结果:
它变成了这样:
最佳答案
可能是!因为我没有看到你与 Canvas 交互的代码,所以我发布了完全有效的代码。逻辑是一行不能覆盖另一行以防正常运行。希望我能帮助你.
// 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;
通过这个逻辑,你可以在这个逻辑中使用任何你想要的东西,它让点击发生而不被覆盖
关于java - 使 Canvas 的画线可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27681467/