学习Android的View体系一定要了解甚至熟练运用动画,才能做出优秀的应用,这里强调是View/ViewGroup体系下的动画,因为又新出了Jetpack Compose体系的动画,后续会总结姊妹篇出来。
import android.animation.Keyframe; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; public class AnimationUtil { public static void startShakeByViewAnim(View view, float scaleSmall, float scallLarge,float shakeDegrees, long duration) { if (view == null) { return; } // 有小变大 Animation scaleAnim = new ScaleAnimation(scaleSmall, scallLarge, scaleSmall, scallLarge); // 从左向右 Animation rotateAnim = new RotateAnimation(-shakeDegrees, shakeDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(duration); rotateAnim.setDuration(duration / 10); rotateAnim.setRepeatMode(Animation.REVERSE); rotateAnim.setRepeatCount(10); AnimationSet smallAnimationSet = new AnimationSet(false); smallAnimationSet.addAnimation(scaleAnim); smallAnimationSet.addAnimation(rotateAnim); view.startAnimation(smallAnimationSet); } public static void startShakeByPropertyAnim(View view, float scaleSmall, float scallLarge, float shakeDegrees, long duration) { if (view == null) { return; } // 先变小后变大 PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X, Keyframe.ofFloat(0f,1.0f), Keyframe.ofFloat(0.25f,scaleSmall), Keyframe.ofFloat(0.5f,scallLarge), Keyframe.ofFloat(0.75f,scallLarge), Keyframe.ofFloat(1.0f,1.0f) ); PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y, Keyframe.ofFloat(0f,1.0f), Keyframe.ofFloat(0.25f,scaleSmall), Keyframe.ofFloat(0.5f,scallLarge), Keyframe.ofFloat(0.75f,scallLarge), Keyframe.ofFloat(1.0f,1.0f) ); // 先往左再往右 PropertyValuesHolder rotatevaluesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION, Keyframe.ofFloat(0f,0f), Keyframe.ofFloat(0.1f,-shakeDegrees), Keyframe.ofFloat(0.2f,shakeDegrees), Keyframe.ofFloat(0.3f,-shakeDegrees), Keyframe.ofFloat(0.4f,shakeDegrees), Keyframe.ofFloat(0.5f,-shakeDegrees), Keyframe.ofFloat(0.6f,shakeDegrees), Keyframe.ofFloat(0.7f,-shakeDegrees), Keyframe.ofFloat(0.8f,shakeDegrees), Keyframe.ofFloat(0.9f,-shakeDegrees), Keyframe.ofFloat(1.f,0f) ); ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder,rotatevaluesHolder); objectAnimator.setDuration(duration); objectAnimator.start(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)