Страницы

воскресенье, 6 сентября 2015 г.

Making fast performance complex animations in android

Recently, I've ran into a problem with animating item's height inside a list for some devices with poor performance. And I've done a library for that. It animates RecyclerView or ListView item's height, but isn't heavy-bounded to that containers - see on github. That animation is also known as expand-collapse animation. Here I describe main problems and solutions founded so far, which is used in library. Library is finished and I'll be very glad if you find some issues or propose pull requests.

So, let's start. To be more clear, I'll write some straightforward code which does what I'm telling about:

ValueAnimator animator = ValueAnimator.ofInt(100, 300);
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
    listViewItem.getLayoutParams().height = (Integer) animation.getAnimatedValue();
    listViewItem.requestLayout();
  }
});
animator.start();
Many libraries, I so before, use this technique. But that is heavy because of requestLayout() - it causes layout and measure steps repeated again each call. From the other side,