Android 如何在Canvas onDraw方法中创建onClick事件

发布于 2024-11-26 23:33:03 字数 1666 浏览 9 评论 0原文

我四处搜寻,仍然找不到好的答案。我创建了自己的类来扩展 imageView,并在 onDraw() 方法中使用画布在图像上绘制圆圈。

我现在想做的是在不同位置的图像上绘制一个按钮,并为其设置一个 onClick 事件,因此当用户按下该按钮时,它将打开一个新的活动。

这是我到目前为止所拥有的。它在正确的位置绘制按钮,但我的 onClick 方法没有触发

@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

        //1.arrayList Points. 2.arrayLists for points in X, 3.arrayList for points in Y
        for(int i=0; i<arrayListPoints.size(); i++){


             Button b = new Button(mContext);
             LinearLayout ll = new LinearLayout(mContext);
             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
             layoutParams.setMargins(alPoints_x.get(i), alPoints_y.get(i), 0, 0);

             ll.addView(b, layoutParams);

            //Measure and layout the linear layout before drawing it
             ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));
             ll.layout(0, 0, MeasureSpec.getSize(b.getMeasuredWidth()), MeasureSpec.getSize(b.getMeasuredHeight()));
             //Finally draw the linear layout on the canvas
             ll.draw(canvas);


            //create an onClick event for the button
            b.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {

                     Toast msg = Toast.makeText(mContext, "button clicked \n", Toast.LENGTH_LONG);
                     msg.show(); 

                 } //end of public void

            });

        }


    invalidate();   

}   //end of onDraw()

i've searched around and still can't find a good answer. I have made my own class that extends an imageView and in the onDraw() method i am using canvas to draw circles onto my image.

What i want to do now though is draw a button onto the image in different places and have an onClick event for it, so when the user presses the button it will open up a new activity..

Here is what I have so far..It draws the buttons in the right locations except my onClick method isn't firing

@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

        //1.arrayList Points. 2.arrayLists for points in X, 3.arrayList for points in Y
        for(int i=0; i<arrayListPoints.size(); i++){


             Button b = new Button(mContext);
             LinearLayout ll = new LinearLayout(mContext);
             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
             layoutParams.setMargins(alPoints_x.get(i), alPoints_y.get(i), 0, 0);

             ll.addView(b, layoutParams);

            //Measure and layout the linear layout before drawing it
             ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));
             ll.layout(0, 0, MeasureSpec.getSize(b.getMeasuredWidth()), MeasureSpec.getSize(b.getMeasuredHeight()));
             //Finally draw the linear layout on the canvas
             ll.draw(canvas);


            //create an onClick event for the button
            b.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {

                     Toast msg = Toast.makeText(mContext, "button clicked \n", Toast.LENGTH_LONG);
                     msg.show(); 

                 } //end of public void

            });

        }


    invalidate();   

}   //end of onDraw()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-12-03 23:33:03

我不得不说这种做法可能是有目的的,但我不认为有什么目的。为什么每次重绘图像视图时都要重新创建并绘制布局?

onClick 侦听器不起作用的原因是因为您的布局从未注入到活动内容中,您只是在绘制它(我没想到这甚至会发挥创造力)也因为布局和按钮没有附加对于任何我很确定它们都会被垃圾收集的东西,这当然与这里无关,只是说一下。

而且每次绘制画布时都会发生这种情况,这对性能或记忆都不利。

是否有某种原因您不只是使用图像视图和按钮创建布局,然后在需要时移动按钮。

如果您坚持这种方法,我能想到的唯一“解决方案”是跟踪图像视图中按钮的矩形,然后检查在绘制按钮的区域内是否“单击”了运动事件。

如果您告诉我们为什么要尝试这样做,我们可能可以提供比此实现更好的解决方案,这似乎确实没有必要或正确,但您可能有一个我想不出的正当理由。

更新

因此,我认为根据您的评论,您想要做的是

将 AbsoluteLayout 容器放在您的地图上(填充父级),或者只是将地图放置在这样的容器中,无论哪种方式。

然后将按钮放入该布局容器中(通过 xml 或以编程方式,并不重要)

然后设置这些按钮的布局 x/y/可见性

I have to say this approach may have a purpose but I don't see it. Why would you recreate and paint a layout each and every time your image view is redrawn?

The reason the onClick listener does not work is because you layout is never injected into the activities content, you are just painting it (which I would not have thought would have even worked so points for creativity) Also since the layout and button are not attached to anything I am pretty sure they would be garbage collected, which of course is irrelevant here, just saying.

Also this is happening every time you draw your canvas, which can't be good for performance or memory.

Is there some reason you are not just creating a layout with your imageview and button and then moving the button when you need to.

If you insist on this approach the only "solution" I can think of would be to keep track of the rect of the button in your image view and then check if a motion event "clicked" inside the area the button was drawn.

If you tell us why you are trying to do this, we can probably offer a better solution than this implementation, which really doesn't seem necessary or right but you might have a valid reason I can't think of.

Update

So I think based on your comments what you want to do is

Put an AbsoluteLayout container over your map (fill parent) or just place the map in such a container, either way.

Then put buttons into that layout container (via xml or programatically, doesn't really matter)

Then set the layout x/y/visibilty of those buttons

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文