Android 如何在 Canvas 的 onDraw 方法中创建 onClick 事件 [英] Android How to create onClick events in Canvas onDraw method

查看:71
本文介绍了Android 如何在 Canvas 的 onDraw 方法中创建 onClick 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经四处搜索,但仍然找不到一个好的答案.我已经创建了自己的类来扩展 imageView,并且在 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.

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

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..

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

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 
", Toast.LENGTH_LONG);
                     msg.show(); 

                 } //end of public void

            });

        }


    invalidate();   

}   //end of onDraw()

推荐答案

我不得不说这种方法可能有目的,但我没有看到它.为什么每次重绘图像视图时都要重新创建和绘制布局?

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?

onClick 侦听器不起作用的原因是因为您的布局从未注入到活动内容中,您只是在绘制它(我没想到它甚至会为创造力点数)另外因为布局和按钮不附加任何东西我很确定它们会被垃圾收集,这在这里当然是无关紧要的,只是说.

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.

更新

所以我认为根据您的评论,您想要做的是

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

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

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

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

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

然后设置这些按钮的布局 x/y/visibilty

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

这篇关于Android 如何在 Canvas 的 onDraw 方法中创建 onClick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆