-1

Hi I'm having trouble understanding how to draw a custom view at runtime. For simplicity sake, say I have a custom view (Box) that extends the View class, and basically all it does is create a square which is configured to do in my onDraw() method. Now I want to be able to draw this custom view in an activity depending on if the user clicks a button on the activity.

So if the user clicks the button once, one box is drawn, clicks again another box is drawn maybe 10 pixels down etc. I searched for a solution but all I can find is people using a pre-made xml with the view and then using an inflator but I dont want to have a pre drawn box if the user hasn't pressed the button.

Also another quick question: When is onDraw() called or do I have to explicitly call

Box box = new Box();
box.onDraw(canvas);

Eclipse says that's a suspicious call, I agree.

4

1 回答 1

2

要制作自定义视图,您必须扩展View类并覆盖 onDraw 方法。然后,当您想要绘制此视图时,您必须将其添加到您的视图层次结构中。这很简单 view.add(new Box())view你的根视图在哪里,这样Android系统会onDraw在每个渲染周期自己调用方法。更多信息请参考官方文档

于 2013-07-26T18:16:27.703 回答