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);
}
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