Android动画的发展史:
Android 3.0之前的版本我们可以使用的动画有两种类型,分别是逐帧动画和补间动画,
Android3.0的时候有发布属性动画
Android4.4的时候有了android.transition(过渡动画)框架
下面我们来看看这四种动画
1、逐帧动画
逐帧动画也叫Drawable Animation,是最简单最直观的动画类型,是人眼的视觉暂留效应。
我们可以制动动画中每一帧对应的图片和持续的时间然后播放动画
两种方法实现逐帧动画
1:首先我们将每一帧的图片放到res/drawable 目录下,然后在res/anim目录中新建一个动画的xml文件
在这个文件中使用<animation-list>标签来定义动画帧序列,使用<item>标签来定义动画的每一帧,并指定帧的持续时间等属性
例如:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"//控制动画是否循环播放 true不循环
>
<item android:drawable="@drawable/blog12" android:duration="100"/>//duration表示每一帧的播放持续时间
<item android:drawable="@drawable/blog12" android:duration="100"/>
<item android:drawable="@drawable/blog12" android:duration="100"/>
</animation-list>
下面代码实现
AnimationDrawable animationDrawable=new AnimationDrawable();
for (int i=0;i<10;i++) {
int id = getResources().getIdentifier("blog12", "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
animationDrawable.addFrame(drawable,120);
}
imageView.setBackgroundDrawable(animationDrawable);
animationDrawable.setOneShot(false);
//接下来需要在某个条件下触发动画,伪代码
AnimationDrawable drawable=(AnimationDrawable)imageView.getBackground();
//开始播放
drawable.start();
//结束播放
drawable.stop();
2、补间动画
补间动画无需定义动画过程中的每一帧,只需要定义动画的开发和结束两个关键帧,并指定动画变化的时间和方式等,然后交给系统进行计算。通过两个关键帧之间插入渐变值来实现平滑过渡。主要有四种效果
①透明Alpha ②Sclae缩放③Translate平移④Rotate旋转
同时补间动画也分为xml和代码两种实现方式
我们先来看看xml方式
<?xml version="1.0" encoding="utf-8" ?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
>
<translate
android:duration="200"
android:fromYDelta="0"
android:fromXDelta="0"
android:toYDelta="0"
android:toXDelta="1000"
/>
<!--toAlpha 1.0表示完全不透明 0.0表示完全透明-->
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"//alpha 取值从0-1
android:duration="2000"
/>
<scale
android:duration="200"
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="2000"
android:fromDegrees="0"
android:pivotY="50%"//旋转中心坐标
android:pivotX="50%"//旋转中心坐标
android:startOffset="0"
android:repeatCount="-1"
android:repeatMode="restart"
android:toDegrees="360"/>
</set>
Alpha:
public void alpha(){
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(100);
animation.setFillAfter(true);
imageView.setAnimation(animation);
}
public void scale(){
ScaleAnimation animation = new ScaleAnimation(
1.0f, 4.0f, 1.0f, 4.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f);
animation.setDuration(100);
animation.setFillAfter(true);
imageView.setAnimation(animation);
}
public void translate(){
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF,
2f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 2f
);
animation.setDuration(100);
animation.setFillAfter(true);
imageView.setAnimation(animation);
}
public void retate(){
RotateAnimation animation = new RotateAnimation(0,
-360,
RotateAnimation.RELATIVE_TO_SELF,
0.5f,
RotateAnimation.RELATIVE_TO_SELF,
0.5f);
animation.setDuration(100);
animation.setFillAfter(true);
imageView.setAnimation(animation);
}
3、属性动画
对于补间动画我们只能改变View的绘制效果,View的真实属性是没有变化的,而属性动画则可以直接改变View对象的属性值
,同时属性动画几乎可以对任何对象执行动画,而不是局限在View上,从某种意义上讲,属性动画可以说是增强版的补间动画。
在看属性动画之前我们先来了解下Evaluator这个类
1、Evaluator是用来控制属性动画如何计算属性值的,接口定义是TypeEvaluator,其中定义了evaluate方法,供不同类型的子类实现。
public interface TypeEvaluator<T>{
public T evaluate(float fraction,T startValue,T endValue);
}
常见的实现类有IntEvaluator ,FloatEvaluator,ArgbEvaluator等。大家可以实际的去了解下各个的源码。
2、AnimatorSet
AnimatorSet也是Animator的子类,用来组合多个Animator,并指定这些Animator是顺序播放还是同时播放。
3、ValueAnimator
ValueAnimator是属性动画最重要的一个类,继承自Animator.它定义了属性动画大部分的核心功能,包括计算各个帧的属性值、
处理更新事件、按照属性值的类型控制计算规则等。
一个完整的属性动画由下面的两个部分组成。
- 计算动画各个帧的相关属性值
- 将这些属性值设置给指定的对象
ValueAnimator为开发者实现了第一部分的功能,第二部分功能由开发者自己来设置
public static ValueAnimator ofInt(int... values) {
ValueAnimator anim = new ValueAnimator();
anim.setIntValues(values);
return anim;
}
public static ValueAnimator ofArgb(int... values) {
ValueAnimator anim = new ValueAnimator();
anim.setIntValues(values);
anim.setEvaluator(ArgbEvaluator.getInstance());
return anim;
}
public static ValueAnimator ofFloat(float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
return anim;
}
public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
ValueAnimator anim = new ValueAnimator();
anim.setValues(values);
return anim;
}
public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
ValueAnimator anim = new ValueAnimator();
anim.setObjectValues(values);
anim.setEvaluator(evaluator);
return anim;
}
或者到以上这些实例后接着需要设置他们的动画持续的时间、插值方式、重复次数等属性值。然后设置AnimatorUpdateListener监听器,并在回调的方法中计算出来的属性值设置给指定对象,最后启动动画。
int color = 0xff00ff;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
color = getWindow().getStatusBarColor();
}
ValueAnimator colorA= ValueAnimator.ofObject(new ArgbEvaluator(),color,inputClor);
colorA.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor((Integer) animation.getAnimatedValue());
}
}
});
colorA.setDuration(500).setStartDelay(100);
colorA.start();
255

被折叠的 条评论
为什么被折叠?



