總的來講,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();
複製代碼