Search code examples
androidanimationtransitionviewflipperlag

Android viewflipper animation lag


I have some basic animations that work like a charm when they are used for transitions between the activities.

The problem is that when I use them on my viewflipper I see a very noticeable lag. Here is a code snippet from my activity:

 private void initUI(){
    layoutInflater = LayoutInflater.from(this);
    flipViewMain = layoutInflater.inflate(R.layout.flip_view_profile_main, null);
    flipListHolder = layoutInflater.inflate(R.layout.flip_view_profile_list, null);
    flipDescriptionHolder  =  layoutInflater.inflate(R.layout.flip_view_profile_description, null);

    profileFlipper = (ViewFlipper) findViewById(R.id.profile_flipper);

    profileFlipper.addView(flipViewMain);
    profileFlipper.addView(flipListHolder);
    profileFlipper.addView(flipDescriptionHolder);

    flipInNextAnimation = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
    flipOutNextAnimation = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
    flipInPreviousAnimation = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
    flipOutPreviousAnimation = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
 }

  private void handleFlip(int position){
    if(position<currentPosition){
        profileFlipper.setInAnimation(flipInPreviousAnimation);
        profileFlipper.setOutAnimation(flipOutPreviousAnimation);
    } else if(position>currentPosition){
        profileFlipper.setInAnimation(flipInNextAnimation);
        profileFlipper.setOutAnimation(flipOutNextAnimation);
    }
    currentPosition = position;

    profileFlipper.setDisplayedChild(position);
}

All animation are like this one:

<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />

 </set>

What am I doing wrong? How can I get rid of this lag? I thought that maybe I would better use a ViewPager but it seems like it is not very flexible in terms of animation modifications. Thanks.


Solution

  • Apparently the problem was not caused by the ViewFlipper. I noticed the animation lag only when debugging on my Galaxy Nexus device. I found out here that:

    The Galaxy Nexus is 1280 x 720 for a total of 921K pixels on screen. So basically, the GPU on the Galaxy Nexus has to draw 2.4 times as many pixels per frame as the it does on the Infuse or S II. The potential frame rate can be expected to be roughly 2.4 times worse on the Galaxy Nexus as it can on the Infuse or Galaxy S II.

    That's the reason for my animation problem. Solved it by enabling hardware acceleration:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        }
        setContentView(R.layout.content);
    
        initUI();
    
    }