【Code-Snippet】Anim

總的來講,Android動畫能夠分爲兩類,最初的傳統動畫和Android3.0 以後出現的屬性動畫; 傳統動畫又包括 幀動畫(Frame Animation)和補間動畫(Tweened Animation)。java

1. View Animator

Animation類 是全部動畫(scale、alpha、translate、rotate)的基類,這裏以scale標籤爲例,講解一下,Animation類所具備的屬性及意義。android

  • android:duration 動畫持續時間,以毫秒爲單位
  • android:fillAfter 若是設置爲true,控件動畫結束時,將保持動畫最後時的狀態
  • android:fillBefore 若是設置爲true,控件動畫結束時,還原到開始動畫前的狀態
  • android:fillEnabled 與android:fillBefore 效果相同,都是在動畫結束時,將控件還原到初始化狀態
  • android:repeatCount 重複次數
  • android:repeatMode 重複類型,有reverse和restart兩個值,reverse表示倒序回放,restart表示從新放一遍,必須與repeatCount一塊兒使用才能看到效果。由於這裏的意義是重複的類型,即回放時的動做。
  • android:interpolator 設定插值器,其實就是指定的動做效果,好比彈跳效果等,不在這小節中講解,後面會單獨列出一單講解。

分類bash

1.1 alpha 漸變透明度動畫效果

  • android:fromAlpha 動畫開始的透明度,從0.0 --1.0 ,0.0表示全透明,1.0表示徹底不透明
  • android:toAlpha 動畫結束時的透明度,也是從0.0 --1.0 ,0.0表示全透明,1.0表示徹底不透明

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);
複製代碼

1.2 scale 漸變尺寸伸縮動畫效果

XML參數說明:動畫

  • android:fromXScale 起始的X方向上相對自身的縮放比例,浮點值,好比1.0表明自身無變化,0.5表明起始時縮小一倍,2.0表明放大一倍;
  • android:toXScale 結尾的X方向上相對自身的縮放比例,浮點值;
  • android:fromYScale 起始的Y方向上相對自身的縮放比例,浮點值,
  • android:toYScale 結尾的Y方向上相對自身的縮放比例,浮點值;
  • android:pivotX 縮放起點X軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p,當爲數值時,表示在當前View(若是當前爲按鍵點擊來切換頁面,則這個View是button)的左上角,即原點處加上50px,作爲起始縮放點;若是是50%,表示在當前控件的左上角加上本身寬度的50%作爲起始點;若是是50%p,那麼就是表示在當前的左上角加上父控件寬度的50%作爲起始點x軸座標。
  • android:pivotY 縮放起點Y軸座標,取值及意義跟android:pivotX同樣。 關於pivotX和pivotY設置爲'%p'的時候的原點爲屏幕的左上角,向右爲x正方向,向下爲y正方向

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);
複製代碼

1.3 translate 畫面轉換位置移動動畫效果

  • android:fromXDelta 起始點X軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p,具體意義已在scale標籤中講述,這裏就再也不重講
  • android:fromYDelta 起始點Y軸從標,能夠是數值、百分數、百分數p 三種樣式;
  • android:toXDelta 結束點X軸座標
  • android:toYDelta 結束點Y軸座標

1.4 rotate 畫面轉移旋轉動畫效果

  • android:fromDegrees 開始旋轉的角度位置,正值表明順時針方向度數,負值代碼逆時針方向度數
  • android:toDegrees 結束時旋轉到的角度位置,正值表明順時針方向度數,負值代碼逆時針方向度數
  • android:pivotX 縮放起點X軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p,具體意義已在scale標籤中講述,這裏就再也不重講
  • android:pivotY 縮放起點Y軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p

1.5 Animation Set

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);
複製代碼

2. Property Animation

Property Animation 也能夠用xml來定義,property的xml放在 res/animator 下。spa

2.1 xml設置

  • alpha
<?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"
     />
複製代碼
  • sets
<?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>
複製代碼
  • Java代碼中調用

載入 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();
複製代碼

2.2 java設置

  • 設置單一的 ObjectAnimator
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,必需要在addUpdateListener中更新控件的屬性,否則沒有任何效果
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
AnimatorSet set = new AnimatorSet();
set.setDuration(1000);
set.play(anim);
set.play(animator);
set.playTogether(anim, animator);    //設置兩個動畫一塊兒運行
set.start();
複製代碼
相關文章
相關標籤/搜索