开发者

Smooth animations for heavy layouts

My application uses standard android TranslateAnimation to slide the views in and out of the screen. Unfortunately the layout seems to be quite heavy: ImageView, bunch of text views and a Gallery with text and images in it. There are two views being animated - one that slides out, and another that slides in.

The problem is in the low performance of these animations, especially on the devices with the less powerful CPU. The animations do not look smooth enough.

I am thinking of removing TranslateAnimations and trying to capture the view contents in a Bitmap and move these as ImageViews.

Do you have any ideas how such tasks should be done properly and if the image moving approach will help?

P.S.

I think that I may be using animations in开发者_StackOverflow a wrong way. I have two views on the FrameLayout. One is visible, the other is not. Then I handle touch events and apply the TranslateAnimations to both views (on ACTION_MOVE) as the user moves the finger along the screen. So it looks like the user moves one view out of the screen while pulling another from the side of the screen. It works fine for lightweight layouts.


You should enable the drawing cache on the animated Views. Look at the documentation for View.setDrawingCacheEnabled(boolean).


Another solution would be to render your views onto 3D surfaces and animate those via OpenGL. That's how Sony do all their nifty animations as seen e.g. in their TimeScape app.

There's a blog post about it.


You must try using view flipper. Its very simple to include your views in it and use animation files to execute numerous animations. This link might be helpful:

viewFlipper for animation


Finally I managed to implement required behavior using the customized Gallery view. It is working perfect now.


As you react to touch events, you likely instantiate new TranslateAnimations in your onTouch method wich is fired very often. Massively creating new instances result in massive garbage collection.

2 solutions:

  1. Implement your own StaticTranslateAnimation and avoid instantiating it on each move event (e.g. setTranslation(int x, int y)). Downside: This approach worked for me only for a single animation. Animating two objects at a time resulted again in garbage collection, although no objects were instantiated (in my code) during the event handling.

  2. Use offsetLeftAndRight(int offset) and offsetTopAndBottom(int offset) instead of animations witch performs much better on old and Android 2.x devices and avoids garbage collection completely.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜