Question

I have two RelativeLayout views. The first one has about three children. The second one has only the first one view as its child. I have two animations, fadeIn and translateIn, which must be applied to the views firstView and secondView respectively. The code below is working. They start and end at the same time, but it's lagging. It's not so smooth. Is there any thing I can do to run them simultaneously and smooth?

Here's the code:

private static final int duration = 500;
protected static boolean ANIMATION_FINISHED;

protected static void onShowAnimation() {

    int width;
    ANIMATION_FINISHED = false;
    width = getScreenWidth();

    final AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
    final TranslateAnimation translateIn = new TranslateAnimation(-width, 0, 1, 1);

    fadeIn.setDuration(duration);
    translateIn.setDuration(duration);

    firstView.startAnimation(translateIn);
    secondView.startAnimation(fadeIn);

    secondView.postDelayed(new Runnable() {

        @Override
        public void run() {
            ANIMATION_FINISHED = true;
        }
    }, duration);
}
Was it helpful?

Solution

animations up to gingerbread always been pretty laggy in Android. In Honeycomb and forward they fixed it with the new framework, but then the developers (that's you) must know which framework to use.

to have a nice smooth hardware accelerated animation you should use the android.animation not the android.view.animation

Here you have the complete guide for the property animation framework: https://developer.android.com/guide/topics/graphics/prop-animation.html

and here is the base class for it: https://developer.android.com/reference/android/animation/package-summary.html

if you need to target devices on gingerbread or below, I suggest you to use the NineOldAndroid library that automatically handles the best possible framework for the device. (Remembering it will still be laggy on 2.3, but at least 3.0+ will be smooth).

ps.: You shouldn't use a postDelayed to know when the animation is over, instead use an Animation Listener

OTHER TIPS

After searching a lot finally i successfully remove lagging during animation.

you need to set,

yourAnimatedView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

before starting animation, and after completion of animation you need to set,

yourAnimatedView.setLayerType(View.LAYER_TYPE_NONE, null);

During animations your views may be redrawn each frame. If you use view layers, instead of having to redraw each frame, views render once into an off-screen buffer which can be reused.

In addition, hardware layers are cached on the GPU, which makes certain operations during animation much faster. Simple transformations (translation, rotation, scaling and alpha) can be rendered quickly with layers. Since many animations are just a combination of these transformations, layers can supercharge animation performance

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top