开发者

How to animate views?

开发者 https://www.devze.com 2023-01-09 12:05 出处:网络
I\'m working on a game that in some ways is similar to Tetris (imagine a 2D array of colored squares that sometimes move around)

I'm working on a game that in some ways is similar to Tetris (imagine a 2D array of colored squares that sometimes move around)

I am trying to animate the individual squares so they will smoothly slide down from coordinate to the next. Since I wanted to use Android's built-in tweening feature, the animation has to apply to the whole View (rather than parts of it). This doesn't work well for me because I only want some of the colored squares to slide down, and the rest of them to stay still.

The (theoretical) solution I came up with to resolve this is to make 2 Views, layered directly on top of each other. The top view is for animating s开发者_开发问答quares when they need to move, and the bottom layer is for the static squares. The animation-layer is transparent until I am ready to animate something. I then simply turn on the colored square in the animation-layer, tween it to the new location, and turn it back off when done. In the same time span, the static-layer just turns squares on and off at the right time to make the whole thing look seamless to the end user.

The proposed solution is just a theory, since I haven't been able to make it work correctly yet. Since I have been having trouble, I was wondering if this is even the best way to solve the problem? Perhaps there is a more elegant solution that I am over looking? Anyone know of a better way?


If you just want to animate a single element check out the namespace android.view.animation.Animation. You can also use Drawable shapes and draw them directly. Finally, if you want a simulation then you will have to look into threading. Basically you will create a timer to update the canvas for you based on an interval. There are some other view canvases you can use as well like the GLView canvas.

0

精彩评论

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