ndr​​oid如何创建的onClick事件在画布的OnDraw

ndr​​oid如何创建的onClick事件在画布的OnDraw

本文介绍了的Andr​​oid如何创建的onClick事件在画布的OnDraw方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜索周围,仍然无法找到一个很好的答案。我已经作出了自己的类,它扩展了ImageView的和的OnDraw()方法,我用画布上画圈圈到我的形象。

我想现在做的虽然是画一个按钮到在不同的地方形象,有一个onclick事件,所以当用户presses的按钮,它会打开一个新的活动。

下面是我这么far..It绘制按钮,除了我的onClick方法正确的位置在不触发

  @覆盖
保护无效的OnDraw(帆布油画){
    super.onDraw(画布);

        //1.arrayList点。 2.arrayLists在x指向,3.arrayList为Y轴点
        的for(int i = 0; I< arrayListPoints.size();我++){


             按钮B =新按钮(mContext);
             的LinearLayout LL =新的LinearLayout(mContext);
             LinearLayout.LayoutParams的LayoutParams =新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);

            //测量和绘图之前布局的线性布局
             ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()),MeasureSpec.getSize(ll.getMeasuredHeight()));
             ll.layout(0,0,MeasureSpec.getSize(b.getMeasuredWidth()),MeasureSpec.getSize(b.getMeasuredHeight()));
             //最后绘制线性布局在画布上
             ll.draw(画布);


            //创建一个onclick事件按钮
            b.setOnClickListener(新OnClickListener(){
                 @覆盖
                 公共无效的onClick(视图v){

                     吐司味精= Toast.makeText(mContext按钮,点击\ N,Toast.LENGTH_LONG);
                     msg.show();

                 公共无效的} //结束

            });

        }


    无效();

OnDraw中的} //结束()
 

解决方案

我不得不说,这种方法可能是有目的的,但我没有看到它。为什么你会重新和油漆的布局每一次你的形象的看法是重绘?

onClick的监听器不工作的原因是因为你的布局从未注入活动的内容,你只是画吧(我不会想到甚至会工作这么创造力点),此外,由于布局和按钮没有附加到任何东西我是pretty的肯定,他们将垃圾回收,这当然这里是无关紧要的,只是说。

另外这个正在发生的事情每次你画你的画布时,它不能很好的性能和内存。

是有一些原因,你不只是建立你的ImageView和按钮布局,然后移动按钮,当您需要。

如果你坚持这种方法唯一的解决方案我能想到的将是跟踪按钮的矩形的图像中的视图,然后检查是否有移动事件点击的区域内绘制按钮。

如果你告诉我们,为什么您要做到这一点,我们或许可以提供比这个实现,这确实似乎没有必要或不正确的一个更好的解决办法,但你可能有一个有效的原因,我不能想到的。

更新

所以,我认为根据您的意见,你想要做的是

把一个AbsoluteLayout容器在你的地图(填充母)或只放置地图在这样的容器中,无论采用哪种方式。

然后把按钮进入该布局的容器(通过XML或编程,其实并不重要)

然后设置这些按钮的布局X / Y /的可见性

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()
解决方案

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

这篇关于的Andr​​oid如何创建的onClick事件在画布的OnDraw方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:44