总的来讲,Android动画能够分为两类,最初的传统动画和Android3.0 以后出现的属性动画; 传统动画又包括 帧动画(Frame Animation)和补间动画(Tweened Animation)。java
Animation类 是全部动画(scale、alpha、translate、rotate)的基类,这里以scale标签为例,讲解一下,Animation类所具备的属性及意义。android
分类:bash
Example:ide
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="5000"
android:fromAlpha="1.0" android:toAlpha="0.1"/>
</set>
复制代码
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.1f);
复制代码
XML参数说明:动画
Example:ui
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="5000"
android:fromXScale="1.0" android:fromYScale="1.0"
android:pivotX="50%" android:pivotY="50%"
android:toXScale="1.5" android:toYScale="1.5"/>
</set>
复制代码
ImageView img = (ImageView) findViewById(R.id.img);
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
img.startAnimation(scaleAnimation);
复制代码
也能够直接用代码设置:this
ScaleAnimation scaleAnim2 = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
复制代码
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.1f);
ScaleAnimation scaleAnim2 = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
xy.addAnimation(alphaAnim);
xy.addAnimation(rotateAnim);
xy.addAnimation(scaleAnim2);
xy.setDuration(3000);
btn_xml.startAnimation(xy);
复制代码
Property Animation 也能够用xml来定义,property的xml放在 res/animator 下。spa
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="0.0"
android:repeatCount="1"
android:repeatMode="reverse"
android:propertyName="alpha"
android:valueType="floatType"
/>
复制代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="0.0"
android:valueType="floatType"
android:propertyName="alpha"
android:repeatCount="1"
android:repeatMode="reverse"/>
<objectAnimator
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="3.0"
android:valueType="floatType"
android:repeatCount="1"
android:repeatMode="reverse"
android:propertyName="scaleX"/>
</set>
复制代码
载入 ObjectAnimator 动画 (1)经过AnimatorInflater.loadAnimator载入, (2)注意须要设置 setTarget (3)和代码同样的道理,属性值也能够在代码中动态修改 (4)新建的动画类的类别必须跟XML文件中的根标签对应rest
ObjectAnimator xxx = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this,
R.animator.test_object_animator);
xxx.setTarget(v);
xxx.start();
复制代码
ObjectAnimator anim = ObjectAnimator //新建ObjectAnimator
.ofFloat(v, "alpha", 1.0f, 0.0f,1.0f) //设置变化的值从3.0f到0.0f
.setDuration(1000); //设置变化时间
anim.setRepeatCount(1); //设置重复次数
anim.setRepeatMode(ObjectAnimator.REVERSE); //设置重复模式
anim.setTarget(v); //绑定控件,能够不用设置
btn_java.setPivotX(0); //设置变化的中心
btn_java.setPivotX(0);
anim.start(); //开启动画
复制代码
ValueAnimator animator = ValueAnimator
.ofFloat(3.0f,0.0f)
.setDuration(1000);
animator.setRepeatCount(1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setTarget(v);
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (Float) animation.getAnimatedValue();
v.setAlpha(x); //设置透明度
v.setScaleX(x); //设置X轴
v.setScaleY(x); //设置Y轴
}
});
复制代码
AnimatorSet set = new AnimatorSet();
set.setDuration(1000);
set.play(anim);
set.play(animator);
set.playTogether(anim, animator); //设置两个动画一块儿运行
set.start();
复制代码