开发者

Android: how to have an animation on top of a background image

开发者 https://www.devze.com 2023-03-30 03:46 出处:网络
What I\'m trying to do is have a background image, for sake of simplicity, lets say it\'s a picture of the front of a house.Then, I want to have a red ball move fromwindow to window.

What I'm trying to do is have a background image, for sake of simplicity, lets say it's a picture of the front of a house. Then, I want to have a red ball move from window to window.

**I want to have a background picture, and a picture on top of it.

**I then want to be able to tell the top picture EXACTLY where to go.

How can I do this?

I'm just beginning to learn about a开发者_运维百科nimations in Android, and have not yet run across any way to do this.


There are two routes to animation in android: Canvas and OpenGL ES. I would recommend OpenGL for anything requiring smoothness and speed, like a moving ball. You should create a view using the helper class GLSurfaceView http://android-developers.blogspot.com/2009/04/introducing-glsurfaceview.html, and implement a Renderer.

I assume you have the images saved in your res/drawable folders, in a format like png and the ball file contains an alpha channel.

You can see many tutorials online, but basically you need to load your background image and your ball resource at onSurfaceCreated and store it in a texture using GLUtils.texImage2D.

In the onDrawFrame method, you should set up a 2D projection such as glOrtho2D, then draw the background.

Then just before you draw the ball texture, you can use the glTranslate(x,y,0) function to move the ball over the house. Use an alpha blend for the ball:

glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA);
glEnable(GL_BLEND);

Unfortunately writing in OpenGL isn't as straightforward as you might hope. Everything is done with 3D coordinates, despite the fact you want only a 2D image. But hopefully this gives you enough info to google for good exmaples, which are abundant!

0

精彩评论

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