简体   繁体   中英

How to add view to class that extends SurfaceView

I'm new to Android and I'm confused on how the views work; just following a tutorial, but I'm trying to deviate from it

So I have an activity, and I have this line in the onCreate: setContentView(new GamePanel(this));

GamePanel is a class that extends SurfaceView; it uses a canvas to draw things onto the screen

I want to add a button to the screen; is there a way to do this? Anything that will provide an onClick method will do...is it possible to draw it on the canvas and have it clickable?

SurfaceView has two parts, the Surface and the View. Avoid extending SurfaceView. Subclassing is useful if you want to draw on the View, but most apps just want to draw on the Surface. If you subclass SurfaceView you'll have access to both, and if that's not what you want it's easy to write code that behaves in strange ways.

If you want View-based UI elements, don't try to put them on the Surface. By default, the Surface sits behind the View UI layer, so putting UI elements on top is automatic.

You can find some examples in Grafika . The "multi-surface test" features multiple Canvas-rendered overlapping SurfaceViews, which is a bit more than you want, but it demonstrates what you need. Other examples have UI elements on top of OpenGL ES rendering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM