Android动画分类
Android提供了三种动画类型:
View Animation: 视图动画在古老的Android版本系统中就已经提供了,只能被用来设置View的动画。
Drawable Animation: 这种动画(也叫Frame动画、帧动画)其实可以划分到视图动画的类别,专门用来一个一个的显示Drawable的resources,就像放幻灯片一样。
Property Animation: 属性动画只对Android 3.0(API 11)以上版本的Android系统才有效,这种动画可以设置给任何Object,包括那些还没有渲染到屏幕上的对象。这种动画是可扩展的,可以让你自定义任何类型和属性的动画。
相关API
相关属性:
Duration:动画的持续时间,默认300ms。
Interpolation:时间差值,定义动画的变化率。
RepeatCount:重复次数,可以定义重复多少次。
RepeatMode:重复模式,重复播放时,是从头开始播还是反向播。
Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
Frame refresh delay:帧刷新延迟,动画多久刷新一次帧;默认为10ms。
相关类:
ValueAnimator 动画的执行类。
ObjectAnimator 动画的执行类,ValueAnimator的子类。
AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater 用户加载属性动画的xml文件。
TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
TimeInterpolator 时间插值。
Animator
Animator是动画属性的基类(父类),我们先看一下它的继承结构:
Android的animation由四种类型组成:
alph 渐变透明度动画效果
scale 渐变尺寸伸缩动画效果
translate 画面转换位置移动动画效果
rotate 画面转移旋转动画效果
视图动画 (View Animation)
Java代码实现
渐变动画
public AlphaAnimation(float fromAlpha, float toAlpha)
float fromAlpha:起始透明度
float toAlpha:结束透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
旋转动画
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
float fromDegrees:旋转的开始角度。
float toDegrees:旋转的结束角度。
int pivotXType:缩放中心点的X坐标类型。取值范围为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotXValue:缩放中心点的X坐标值。当pivotXType==ABSOLUTE时,表示绝对位置;否则表示相对位置,progress_1.0表示100%。
int pivotYType:缩放中心点的Y坐标值。
float pivotYValue:缩放中心点的Y坐标类型。
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.7f,
Animation.RELATIVE_TO_SELF, 0.7f);
rotateAnimation.setDuration(2000);
animationSet = new AnimationSet(true);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
尺寸伸缩
public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromX:动画开始前在X坐标的大小。
toX:动画结束后在X坐标的大小。
fromY:动画开始前在Y坐标的大小。
toY:动画结束后在Y坐标的大小。
pivotXType:缩放中心点的X坐标类型。取值范围为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotXValue:缩放中心点的X坐标值。当pivotXType==ABSOLUTE时,表示绝对位置;否则表示相对位置,progress_1.0表示100%。
pivotYType:缩放中心点的Y坐标类型。
pivotYValue:缩放中心点的Y坐标。
ScaleAnimation scaleAnimation = new ScaleAnimation(0,2f, 0,2f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f );
scaleAnimation.setDuration(2000);
animationSet = new AnimationSet(true);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
转移动画
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
fromXDelta:起始X坐标
toXDelta: 结束X坐标
fromYDelta:起始Y坐标
toYDelta: 结束Y坐标
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -500 );
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);//结束停在哪里了
translateAnimation.setRepeatCount(3);//动画重复次数
animationSet = new AnimationSet(true);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(translateAnimation);
XML实现
anim_alpha.xml
?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
<!--
透明度控制动画效果 alpha
浮点型值:
fromAlpha 属性为动画起始时透明度
toAlpha 属性为动画结束时透明度
说明:
0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之间的float数据类型的数字
长整型值:
duration 属性为动画持续时间
说明:
时间以毫秒为单位
-->
anim_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+350" />
</set>
<!--
rotate 旋转动画效果
属性:interpolator 指定一个动画的插入器
在我试验过程中,使用android.res.anim中的资源时候发现
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速- 动画插入器
其他的属于特定的动画效果
浮点数型值:
fromDegrees 属性为动画起始时物件的角度
toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
说明:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
说明: 以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
-->
anim_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4" />
</set>
<!--
尺寸伸缩动画效果 scale
属性:interpolator 指定一个动画的插入器
在我试验过程中,使用android.res.anim中的资源时候发现
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速- 动画插入器
其他的属于特定的动画效果
浮点型值:
fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
toXScale 属性为动画结束时 X坐标上的伸缩尺寸
fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
toYScale 属性为动画结束时Y坐标上的伸缩尺寸
说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
说明:
以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
布尔型值:
fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-->
anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="30"
android:fromYDelta="30"
android:toXDelta="-80"
android:toYDelta="300" />
</set>
<!--
translate 位置转移动画效果
整型值:
fromXDelta 属性为动画起始时 X坐标上的位置
toXDelta 属性为动画结束时 X坐标上的位置
fromYDelta 属性为动画起始时 Y坐标上的位置
toYDelta 属性为动画结束时 Y坐标上的位置
注意:
没有指定fromXType toXType fromYType toYType 时候,
默认是以自己为相对参照物
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
-->
MainActivity.class
public void onClick(View v) {
Animation animation = null;
switch (v.getId()){
case R.id.btn_alpha:
animation = AnimationUtils.loadAnimation(this,R.anim.anim_alpha);
imageView.startAnimation(animation);
break;
case R.id.btn_rotate:
animation = AnimationUtils.loadAnimation(this,R.anim.anim_rotate);
imageView.startAnimation(animation);
break;
case R.id.btn_scale:
animation = AnimationUtils.loadAnimation(this,R.anim.anim_scale);
imageView.startAnimation(animation);
break;
case R.id.btn_translate:
animation = AnimationUtils.loadAnimation(this,R.anim.anim_translate);
imageView.startAnimation(animation);
break;
}
旋转动画不卡顿
xml
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="restart"
android:pivotX="50%"
android:pivotY="50%" />
MainActivity.class
//加载布局
Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_rotate);
//时间差值,定义动画的变化率 这里是加速
animation.setInterpolator(new LinearInterpolator());
imageView.startAnimation(animation);
逐帧动画 (FrameAnimation)
在drawable里新建anim_loading.xml
oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_1" android:duration="150"></item>
<item android:drawable="@drawable/loading_2" android:duration="150"></item>
<item android:drawable="@drawable/loading_3" android:duration="150"></item>
<item android:drawable="@drawable/loading_4" android:duration="150"></item>
<item android:drawable="@drawable/loading_5" android:duration="150"></item>
<item android:drawable="@drawable/loading_6" android:duration="150"></item>
</animation-list>
MainActivity.class
imageView.setImageResource(R.drawable.anim_loading);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
视图动画
https://www.cnblogs.com/yc-755909659/p/4290114.html
旋转动画不停旋转
https://blog.csdn.net/dreamer0924/article/details/16855995
逐帧动画
https://www.jianshu.com/p/7ba70e061bb4