帧动画——FrameAnimation
将一系列图片有序播放,形成动画的效果。其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用
在Drawable文件夹下,创建animation-list为根节点的资源文件
oneshot:是否只播放一次
drawable:一帧引用的图片
duration:一帧播放的时间
播放动画
将动画作为控件的背景
((AnimationDrawable)view.getBackground()).start();
Animation常用属性
duration:动画时间
repeatCount:重复次数 infinite无限次
fillAfter:是否停止在最后一帧
repeatMode:重复模式 值:restart重新开始,reserve反复
startOffset:开始延迟时间
补间动画 Tween Animation
只能应用于View对象,只支持部分属性,View animation值改变了View绘制的位置,并没有改变对象本身的真实位置
可以使用XML定义也可以使用代码定义 XML定义的动画放在/res/anim/文件夹内
开始动画 通过view的startAnimation(Animation a) 参数定义的动画
四种补间动画通过XML定义
AlphaAnimation:透明度动画
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000">
AlphaAnimation alphaAnimation=null;
//加载XML中的动画XML文件
alphaAnimation= (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_alpha);
//常用属性设置 各种动画通用
alphaAnimation.setRepeatCount(3);//执行动画效果结束后重复执行3次 一共4次
alphaAnimation.setRepeatMode(Animation.REVERSE);//重复模式
//动画结束是否停止在最后一帧
alphaAnimation.setFillAfter(true);
//动画结束是否停止在第一帧
alphaAnimation.setFillBefore(false);
//设置插值器 动画执行速度 变速 加减速。。
//AccelerateInterpolator减速
//DecelerateInterpolator加速
alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
ScaleAnimation:缩放动画
代码加载的方式和方法的使用与AlphaAnimation一样
android:toXScale="1"
android:toYScale="1"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotY="0%"
android:pivotX="0%"
android:duration="2000">
TranslateAnimation:平移动画
android:fromXDelta="-100%p"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="0"
android:duration="2000">
RotateAnimation:旋转动画
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
通过构造方法创建
//在代码中定义 动画实例对象
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;
//根据各自的构造方法来初始化一个实例对象
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation
AnimationAlphaAnimation(float fromAlpha, float toAlpha)
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
//说明:
// 0.0表示完全透明
// 1.0表示完全不透明
myAnimation_Alpha.setDuration(5000);
//设置时间持续时间为 5000毫秒
ScaleAnimation
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坐标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Scale.setDuration(700);
//设置时间持续时间为 700毫秒
TranslateAnimation
TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
RotateAnimation
RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个参数fromDegrees为动画起始时的旋转角度
//第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
属性动画
相对补间动画 属性动画会真正的使目标对象的属性值发生改变,不像补间动画只是影像的改变 只能修改具有get/set方法的属性值
因为可以修改对象的属性,属性动画可以做到更多的效果,改变文本大小,背景颜色等等
属性动画创建在 res/animator
ValueAnimator
包含属性动画的所有核心功能,动画时间,开始、结束属性值,属性值计算方法等。
ValuAnimiator设置开始结束值 实现ValueAnimator.onUpdateListener接口,
这个接口只有一个函数onAnimationUpdate(),在这个函数中会传入ValueAnimator对象做为参数,通过这个ValueAnimator对象的getAnimatedValue()函数可以得到当前的属性值
把属性值设置给某个控件的某个属性
使用xml
android:duration="1000"
android:valueFrom="0"
android:valueTo="300"
android:valueType="intType"
android:interpolator="@android:interpolator/overshoot">
ValueAnimator valueAnimator=null;
//通过AnimatorInflater.loadAnimator()加载xml 创建ValueAnimator
valueAnimator= (ValueAnimator) AnimatorInflater.loadAnimator(this,R.animator.animator_value);
//动画执行时间
valueAnimator.setDuration(3000);
//值改变监听
valueAnimator.addUpdateListener(listener);
//开始动画
valueAnimator.start();
private ValueAnimator.AnimatorUpdateListener listener=new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//获取值
int value= (int) animation.getAnimatedValue();
//btnValueAnimator为测试控件
//设置控件X轴平移
btnValueAnimator.setTranslationX(value);
}
};
使用代码
/**
* valueAnimator 单个值
*/
//代码创建 ValueAnimator类自身的方法
//ofFloat值类型float
ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,1);
//ofInt值类型int 从0~300
valueAnimator=ValueAnimator.ofInt(0,300);
//也可以用来设置颜色 在颜色改变过程中会将颜色变化情况显示出来
//红色到蓝色的改变过程 显示N种颜色
valueAnimator=ValueAnimator.ofInt(Color.RED,Color.BLUE);
//ofArgb设置颜色 如果无法使用 是的sdk版本低了
//这个方法改变颜色过程中只显示红色和蓝色
//valueAnimator=ValueAnimator.ofArgb(Color.RED,Color.BLUE);
//设置插值器
valueAnimator.setInterpolator(new CycleInterpolator());
/**
*
* ValueAnimator.ofPropertyValuesHolder 设置多个值
*/
//设置动画属性 参数1:名字 参数2,3值的变化区间
PropertyValuesHolder alphaHolder=PropertyValuesHolder.ofFloat("alpha",0f,1f);
PropertyValuesHolder widthHolder=PropertyValuesHolder.ofInt("width",0,300);
//ValueAnimator.ofPropertyValuesHolder 添加holder 创建动画
valueAnimator=ValueAnimator.ofPropertyValuesHolder(alphaHolder,widthHolder);
//动画执行时间
valueAnimator.setDuration(3000);
//值改变监听
valueAnimator.addUpdateListener(listener);
private ValueAnimator.AnimatorUpdateListener listener=new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
/**
* 单个值获取 getAnimatedValue取出变化值 根据设置类型强转
* btnValueAnimator 测试用的button
*/
// int value= (int) animation.getAnimatedValue();
// btnValueAnimator.setTranslationX(value);横坐标平移
// float value= (float) valueAnimator.getAnimatedValue();
// btnValueAnimator.setAlpha(value);透明度改变
// int value= (int) animation.getAnimatedValue();
// btnValueAnimator.setTextColor(value);文字颜色改变
/**
* PropertyValuesHolder存了多个值 通过名字获取 强制转换
*/
float alpha= (float) valueAnimator.getAnimatedValue("alpha");
int width= (int) valueAnimator.getAnimatedValue("width");
btnValueAnimator.setAlpha(alpha);//改变透明度
//图像绘制 左边不变从右边慢慢增加
//修改控件的width height不能使用setWidth或setHeight
btnValueAnimator.setRight(width);
//btnValueAnimator.setBottom(width);
}
};
ObjectAnimator:
继承自ValueAnimator,要指定一个对象及该对象的一个属性,当属性值计算完成时自动设置为该对象的相应属性,不需要设置监听,底层自动完成,一般会用ObjectAnimator来改变某一对象的某一属性
//用来测试的button
Button btnObjectAnimator= (Button) findViewById(R.id.btn_object_animator);
//加载动画
ObjectAnimator objectAnimator= (ObjectAnimator) AnimatorInflater.loadAnimator(this,R.animator.animator_object);
//绑定控件
objectAnimator.setTarget(btnObjectAnimator);
//参数1 绑定控件 参数2 设置 属性 参数3 设置值
objectAnimator=ObjectAnimator.ofInt(btnObjectAnimator,"textColor",Color.RED);
//PropertyValuesHolder设置多个属性
PropertyValuesHolder translationXHolder=PropertyValuesHolder.ofFloat("translationX",0,300);
PropertyValuesHolder translationYHolder=PropertyValuesHolder.ofFloat("translationY",0,200);
objectAnimator=ObjectAnimator.ofPropertyValuesHolder(btnObjectAnimator,translationXHolder,translationYHolder);
objectAnimator.setDuration(3000);
//开始动画
objectAnimator.start();
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!