Alex Rivera | Logout

Smooth animations for heavy layouts

Asked 2011-03-25T09:26:30.803
10

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 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.

Edit
Report

1 Answer

0

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.

answered 2013-02-06T15:54:41.173

Your Answer