开发者

Android How to create onClick events in Canvas onDraw method

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.arrayLi开发者_JAVA技巧st 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜