补件动画 / 属性动画
程序员文章站
2022-03-17 13:47:57
...
属性动画:
/**
* 属性动画
* 移动:translationX ,translationY
* 旋转: rotation
* 缩放: scaleX , scaleY
* 渐变: alpha
* */
//渐变
ObjectAnimator animator =ObjectAnimator.ofFloat(number_sign,"alpha",1.0f,0.8f,0.6f,0.4f,0.2f,0.4f,0.6f,0.8f,1f);
animator.setDuration(2000);
animator.setRepeatCount(-1);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
//缩放
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(number_sign, "scaleX", 0, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(number_sign, "scaleY", 0, 1f);
animatorSetsuofang.setDuration(2000);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
//移动
ObjectAnimator translationX =ObjectAnimator.ofFloat(number_sign,"translationX",0,0);
ObjectAnimator translationY =ObjectAnimator.ofFloat(number_sign,"translationY",0,600f);
AnimatorSet set =new AnimatorSet();
set.playTogether(translationX,translationY);
set.setDuration(3000);
set.start();
//旋转
ObjectAnimator objectAnimator =ObjectAnimator.ofFloat(home_iv_sport,"rotation",0f,360f);
objectAnimator.setDuration(1000);
objectAnimator.setRepeatCount(-1);
objectAnimator.setRepeatMode(ValueAnimator.RESTART);
//组合
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(home_iv_add, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(home_iv_add, "scaleX", 0.0f, 1.0f);
ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(home_iv_add, "scaleY", 0.0f, 2.0f);
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(home_iv_add, "rotation", 0, 360);
ObjectAnimator transXAnim = ObjectAnimator.ofFloat(home_iv_add, "translationX", 100, 400);
@SuppressLint("ObjectAnimatorBinding")
ObjectAnimator transYAnim = ObjectAnimator.ofFloat(home_iv_add, "tranlsationY", 100, 750);
AnimatorSet set = new AnimatorSet();
set.playTogether(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
//// set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
set.setDuration(3000);
set.start();
补件动画
AlphaAnimation:透明度渐变效果,创建时许指定开始以及结束透明度,还有动画的持续 时间,透明度的变化范围(0,1),0是完全透明,1是完全不透明;对应<alpha/>标签!
ScaleAnimation:缩放渐变效果,创建时需指定开始以及结束的缩放比,以及缩放参考点, 还有动画的持续时间;对应<scale/>标签!
TranslateAnimation:位移渐变效果,创建时指定起始以及结束位置,并指定动画的持续 时间即可;对应<translate/>标签!
RotateAnimation:旋转渐变效果,创建时指定动画起始以及结束的旋转角度,以及动画 持续时间和旋转的轴心;对应<rotate/>标签
AnimationSet:组合渐变,就是前面多种渐变的组合,对应<set/>标签
AlphaAnimation(透明度渐变)
fromAlpha :起始透明度
toAlpha:结束透明度
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
ScaleAnimation(缩放渐变)
fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
TranslateAnimation(位移渐变)
fromXDelta/fromYDelta:动画起始位置的X/Y坐标
toXDelta/toYDelta:动画结束位置的X/Y坐标
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>
RotateAnimation(旋转渐变)
fromDegrees/toDegrees:旋转的起始/结束角度
repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
AnimationSet(组合渐变)
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
调用:
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);