android 动画优化
在做的项目有太多动画 一天下来手机就很卡 优化方案:
先看我之前写的代码:
ObjectAnimator animator = ObjectAnimator.ofFloat(innerView, "translationY", 0f, 30f, 0f);
animator.setDuration(400).setInterpolator(new DampInterpolator());
animator.start();
然后换成这种:
private PropertyValuesHolder animatorX; private PropertyValuesHolder animatorY; animatorX = PropertyValuesHolder.ofFloat("scaleX", 0f, 1.0f); animatorY = PropertyValuesHolder.ofFloat("scaleY", 0f, 1.0f); ObjectAnimator.ofPropertyValuesHolder(view, animatorX, animatorY).start();
之前代码:
AnimatorSet animSet = new AnimatorSet(); ObjectAnimator transYFirstAnim = ObjectAnimator.ofFloat(mView, "translationY", 0, 100); ObjectAnimator transYSecondAnim = ObjectAnimator.ofFloat(mView, "translationY", 100, 0); animSet.playSequentially(transYFirstAnim, transYSecondAnim);
换成
Keyframe k0 = Keyframe.ofFloat(0f, 0); //第一个参数为“何时”,第二个参数为“何地” Keyframe k1 = Keyframe.ofFloat(0.5f, 100); Keyframe k2 = Keyframe.ofFloat(1f, 0); PropertyValuesHolder p = PropertyValuesHolder.ofKeyframe("translationY", k0, k1, k2); ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mView, p); objectAnimator.start();