ValueAnimator lag when set margin changed

799 views Asked by At

I use the following class to play a animation in my app. This works well when the view tree is simple. While, it stuck when I use it in a complex fragment (i.e. the view maybe overdrawed). Is there any way to optimize the efficiency ? I have heard about use surface view and control the redraw district of my Activity, but I need some examples. Thanks a lot.

This is my code

private ValueAnimator performAnim(final View target1, final int start, final int end, final int duration) {
    ValueAnimator valueAnimator = ValueAnimator.ofInt(0, duration);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        private IntEvaluator mEvaluator = new IntEvaluator();

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            Integer currentValue = (Integer) animator.getAnimatedValue();
            float rate = currentValue/((float)duration);
            Log.d("zcc", "currentValue" + currentValue);
            int value = mEvaluator.evaluate(rate, start, end);
            Log.d("zcc", "+" + value + "start : "+ start + " end : "+ end + " duration : " + duration);
            ((LayoutParams) target1.getLayoutParams()).leftMargin = value;
            target1.requestLayout();
        }
    });

    valueAnimator.setDuration(duration);
    return valueAnimator;
}
1

There are 1 answers

0
zccneil On BEST ANSWER

requestLayout() method will measure and layout the views. Thus, the efficiency is quite low. invalid() method, which I use now, will only redraw the view I changed. Hope this may help others