简体   繁体   中英

OpenGL and 9patch drawables

I'm making a brickbreaker game in android with opengl, and I decided I'd wish to use 9patch drawables, so for example a brick could scale up to be wider or taller easilly. Is this possible?

Also, I made my images, and then used the draw9patch.exe in the android tools folder to make the images. It was quite some work and now it turned out it added a few pixels to each side, so the images aren't powers of two anymore, which they must be.

So how do I use nine patch drawables with openGL?

So how do I use nine patch drawables with openGL?

You don't. 9-patches and drawables are for use with the regular Android View classes, not with OpenGL. If you want to have similar behavior, you're going to have to implement your own OpenGL rendering logic including parsing 9-patch PNGs.

It looks like NinePatchDrawable objects don't let you access the underlying bitmaps, but I suppose you could draw them to a Canvas and then read the resulting bitmap out of that. This would probably solve your power-of-two problem as well.

You can then read the padding area with getPadding() , and convert those positions to a 0..1 range for use as texture coordinates.

Then, in OpenGL you would draw a textured brick using a mesh of nine quads, like a '#' symbol. Your texture coordinates for the vertices would be fixed, and correspond to your nine-patch padding. Your position coordinates would depend on the scaling of your block. You would keep your corner quads the same size, and scale the centre quad as desired. (Remaining quads would scale along one dimension as required to keep everything joined up).

Personally, I'd be inclined to forget about the Android nine-patches and instead adopt a convention for the brick artwork where (say) the middle 50% of the width and height are stretchable, and the 25% sections around the perimeter are not. Depends how much work you're throwing away, though.

so the images aren't powers of two anymore, which they must be.

If you use OpenGL-ES 2 that limitation has been lifted. Textures may be of arbitrary size.

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