TweenAnimation 補間動畫

這裏有幾篇比較好的相關文章:

Animation 動畫詳解(一)——alpha、scale、translate、rotate、set的xml屬性及用法 

Animation動畫詳解(二)——Interpolator插值器 

Animation動畫詳解(三)—— 代碼生成alpha、scale、translate、rotate、set及插值器動畫


有4種動畫       TranslateAnimation 、 ScaleAnimation、AlphaAnimation、RotateAnimation

一個動畫集合 AnimationSet

多種Interpolator  插補器

一個工具類AnimationUtils   可以將xml定義的動畫加載到java代碼中

兩種方式定義補間動畫:

1、java代碼中 創建動畫對象  並設置參數

2、在res/anim/中定義  xml的動畫

java代碼中創建動畫:

1)平移動畫

TranslateAnimation ta = new TranslateAnimation(0, 200, 50, 50);
//有兩種構造方法:
// 1、  TranslateAnimation ta = new TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelta);
//delta    希臘字母, 其大寫爲Δ,小寫爲δ。 在數學或者物理中大寫的Δ用來表示增量符號。在原來的基礎上的偏移
//例如:該view 的原始位置是X= 100 ; Y= 100;  那上面的值 相對於屏幕的值就是  100+0;100+200;100 +50;100+50;

/*
TranslateAnimation ta = new TranslateAnimation(int fromXType ,float fromXValue,int toXType ,float toXValue,
int fromYType ,float fromYValue, int toYType ,float toYValue);
fromXType 的值有 Animation.RELATIVE_TO_PARENT   和 Animation.RELATIVE_TO_SELF
以自己  的原始  座標點  加上 多少倍的   自己或者父控件 寬或者 高

例如: view 的 原始座標爲  x =100  y=100   高=200  寬 = 300    父控件的  寬是  600     高  是800
如果fromXType 爲 Animation.RELATIVE_TO_SELF
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF ,0.3f ,Animation.RELATIVE_TO_SELF,1f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,2f);
動畫起始位置x = 100 + 0.3() * 300(寬)      y = 100+ 0.5*200(高);
動畫的終止位置        x =100 +1 * 300     y = 100 + 2 *200

如果fromXType 爲 Animation.RELATIVE_TO_PARENT    那就這裏的寬高都換成 父控件的值
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT ,0.3f ,Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,2f);
動畫起始位置x = 100 + 0.3 * 600(寬)      y = 100+ 0.5*800(高);
動畫的終止位置        x =100 +1 * 600     y = 100 + 2 *800
*/
// 總結:上面兩種方法都是 相對應與其實位置的座標  加上偏移值  獲得新的座標值,
// 前一個是移動具體的像素  後一個是移動多少倍個自己尺寸或者父控件的尺寸
ta.setDuration(2000);  //1次動畫持續的時間
ta.setRepeatCount(1);  //動畫播放的次數
ta.setRepeatMode(Animation.REVERSE);  //重複的動畫是和原先的動畫反着的
ta.setFillAfter(true);//就是view最最終顯示在最後結束動畫的地方
// ta.setFillBefore(true);//就是view最最終顯示在開始動畫的地方
ta.setStartOffset(2000); //開始後 延時幾毫秒  開始動畫
ta.setInterpolator(new BounceInterpolator()); //添加一個 插補器
//插補器  就是以時間爲單位   控制動畫的位置     以便動畫不是勻速的移動
ivJack.startAnimation(ta);  //開始動畫

2)縮放動畫

/*
   以view的中心點 爲中心進行縮放  寬度 10 變成50   高10 變成 50
    public ScaleAnimation(float fromX, float toX, float fromY, float toY);
    public ScaleAnimation(10, 50, 10, 50);
   //下面的方法  就是設置縮放的的中點   和translateAnimation差不多
    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
     float pivotXValue, float pivotYValue);
    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
    int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
*/
3)旋轉動畫

 /*
 有三個構造方法:
     以該view的中心爲 原點進行旋轉    從多少度旋轉到多少度
     public RotateAnimation(float fromDegrees, float toDegrees);

     以X + pivotX  ; Y + pivotY爲旋轉的圓心
     public RotateAnimation(float fromDegrees, float toDegrees,int pivotX,int pivotY);

     以 X + 多少倍的自身或者父控件的寬 ; Y+ 多少倍的自身或者父控件的高  爲旋轉的圓心
     public RotateAnimation(float fromDegrees, float toDegrees,
     int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
*/
     RotateAnimation ra = new RotateAnimation(0f,180f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
     ra.setDuration(2000);
     ra.setFillAfter(true);
     ivJack.startAnimation(ra);
4)透明度動畫Alpha

5) 動畫集合

AnimationSet  set  = new AnimationSet(false);//幾個動畫不共享同一個插補器
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
ivJack.startAnimation(set);
該動畫集合有一個特點: 集合中的動畫是一起播放的 ,如果想要 有序的播放就要 給每個動畫設置 延時播放就是setStartOffset()  就可以了。


第二種方式  在XML文件中創建動畫

1)創建 xml動畫文件



\res\anim\scale_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.5"
       android:toXScale="2"
       android:fromYScale="0.5"
       android:toYScale="2"
       android:duration="4000"
       android:interpolator="@android:interpolator/bounce"
       android:fillAfter="true"
       android:repeatCount="1"
       android:repeatMode="reverse"
       android:pivotX="30%p"
       android:pivotY="30%p"
       android:startOffset="1000"
    >
</scale>

2)java代碼中加載

ScaleAnimation sa = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_animation);
ivJack.startAnimation(sa);


當然通過XML  也可以創建其他動畫 :

       TranslateAnimation 、 ScaleAnimation、AlphaAnimation、RotateAnimation  、AnimationSet、LayoutAnimation(佈局動畫),Interpolator

AnimationUtils.loadAnimation(context,resID); 將xml文件 加載爲動畫
AnimationUtils.loadInterpolator(context, resID); 將xml文件 加載爲插補器
AnimationUtils.loadLayoutAnimation(context,resID); 將xml文件 加載爲佈局動畫

兩種方式效果是一樣的,個人認爲XML 文件的形式跟方便!

 學習補間動畫 關鍵是搞懂 那些參數如何設置       使用很簡單。

補間動畫是不斷通過onDraw()方法來繪製 view  使我們看到有動畫的效果  , 事件上他一直在“原地沒有動過”,設置點擊事件就可以觀察過結果了。

實際開發中 還是建議多用屬性動畫   他是真實的改變了view的位置 ,改變了view的相關屬性------所以叫屬性動畫。

只要View提供了set  和get方法 的屬性,  都可以通過屬性動畫來改變

性能上 屬性動畫由於補間動畫       樣式上也比補間動畫多      就是使用不是很方法便

所以 簡單的動畫   不需要觸摸的動畫用   補間動畫  方便  複雜的動畫  使用  屬性動畫!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章