android 動畫屬性Animation

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

Animation

  在android 程式當中很多時候要用到動畫效果,而動畫效果主要是Animation來實現的,API給出的解釋:

  

 其中包含4種動畫效果

 

  AlphaAnimation 漸層透明度

 

  RotateAnimation 畫面旋轉

 

  ScaleAnimation 漸層尺寸縮放

 

  TranslateAnimation 位置移動

 

 但如果你想把這些動畫效果聯合起來就需要用到一個類AnimationSet  動畫集。

 下面就對這幾個類進行一個簡單的解釋:

  AlphaAnimation 的例子

1     Animation alpha = new AlphaAnimation(0.1f, 1.0f);2             alpha.setDuration(2000);3             image.startAnimation(alpha);

  透明度從0.1f變化至1.0f, 變化所需要的事件為2s;

  RotateAnimation 的例子

1 Animation rotate = new RotateAnimation(0f, 360f);2             rotate.setDuration(2000);3             image.startAnimation(rotate);

  從0度旋轉至360度,期間是2S

  ScaleAnimation 的例子:

Animation scale = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);            scale.setDuration(2000);            image.startAnimation(scale);

     X軸上的長度從0.1變化至1.0f(view原本的寬度)

  Y軸上的長度從0.1變化至1.0f(view原本的長度)

  TranslateAnimation 的例子:

1 Animation translate = new TranslateAnimation(2                     Animation.RELATIVE_TO_SELF, 1.0f,3                     Animation.RELATIVE_TO_SELF, 0.0f,4                     Animation.RELATIVE_TO_SELF, 1.0f,5                     Animation.RELATIVE_TO_SELF, 0.0f);6             translate.setDuration(2000);7             image.startAnimation(translate);

  AnimationSet的例子:

Animation rotate1 = new RotateAnimation(0f, 360f);            Animation translate1 = new TranslateAnimation(                    Animation.RELATIVE_TO_SELF, 1.0f,                    Animation.RELATIVE_TO_SELF, 0.0f,                    Animation.RELATIVE_TO_SELF, 1.0f,                    Animation.RELATIVE_TO_SELF, 0.0f);            AnimationSet set = new AnimationSet(true);            set.addAnimation(rotate1);            set.addAnimation(translate1);            set.setDuration(2000);            image.startAnimation(set);

除了再代碼中添加動畫外,還可以直接在xml檔案中定義好動畫,在直接調用就OK了,例如:

1     <?xml version="1.0" encoding="utf-8"?>  2     <set xmlns:android="http://schemas.android.com/apk/res/android">  3         <alpha  4             android:fromAlpha="0.1"  5             android:toAlpha="1.0"  6             android:duration="2000"  7         />  8     </set>  

  程式碼效果:

  每個按鈕對應不同的動畫

  

  

  代碼下載:下載

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.