Android UI開發第十二篇——動畫效果Animation

來源:互聯網
上載者:User

Android架構本身就使用了大量的動畫效果,比如Activity切換的動畫效果,Dialog彈出和關閉時的漸層動畫效果以及Toast顯示資訊時的淡入淡出效果等等。Android系統架構為我們提供了一些動畫類及其工具類,所以在Andorid應用中使用動畫效果非常簡單。Android中可以在xml中定義Animation,也可以在java code中定義。

Android中動畫的實現分兩種方式,一種方式是補間動畫 Tween Animation,就是說你定義一個開始和結束,中間的部分由android自身實現。另一種叫逐幀動畫 Frame Animation,就是說一幀一幀的連起來播放就變成了動畫。

一、Tween Animation

xml中實現:

alpha 漸層透明度動畫效果
scale 漸層尺寸伸縮動畫效果
translate 畫面轉換位置移動動畫效果
rotate 畫面轉移旋轉動畫效果


JavaCode中 

AlphaAnimation 漸層透明度動畫效果
ScaleAnimation 漸層尺寸伸縮動畫效果
TranslateAnimation 畫面轉換位置移動動畫效果
RotateAnimation 畫面轉移旋轉動畫效果

使用XML檔案定義Tween Animation時XML檔案的根節點可以是<alpha>、<scale> <translate>、<rotate>或者是把它們都放入<set>節點中。如下:

<?xml version="1.0" encoding="utf-8"?>< set xmlns:android="http://schemas.android.com/apk/res/android">  <alpha/>  <scale/>  <translate/>  <rotate/>< /set>

Java Code實現如下:

AlphaAnimation:

AnimationSet animationSet = new AnimationSet(true);//建立一個AnimationSet對象    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//建立一個AlphaAnimation對象             alphaAnimation.setDuration(1000);//設定動畫執行的時間(單位:毫秒)         animationSet.addAnimation(alphaAnimation);//將AlphaAnimation對象添加到AnimationSet當中          view.startAnimation(animationSet);//使用view的startAnimation方法開始執行動畫      

RotateAnimation :

AnimationSet animationSet = new AnimationSet(true);  /**  * 前兩個參數定義旋轉的起始和結束的度數,後兩個參數定義圓心的位置  */  RotateAnimation rotateAnimation = new RotateAnimation(0, 360,                          Animation.RELATIVE_TO_PARENT, 1f,                          Animation.RELATIVE_TO_PARENT, 0f);          rotateAnimation.setDuration(5000);          animationSet.addAnimation(rotateAnimation);          imageView.startAnimation(animationSet); 

TranslateAnimation:

AnimationSet animationSet = new AnimationSet(true);      /**               * x和y軸的起始和結束位置  */  TranslateAnimation translateAnimation = new TranslateAnimation                  (                          Animation.RELATIVE_TO_SELF, 0f,                           Animation.RELATIVE_TO_SELF,0.5f,                           Animation.RELATIVE_TO_SELF, 0f,                          Animation.RELATIVE_TO_SELF, 1.0f                  );                  translateAnimation.setDuration(1000);                  animationSet.addAnimation(translateAnimation);      view.startAnimation(animationSet);    

ScaleAnimation:

AnimationSet animationSet = new AnimationSet(true);      /**               * 圍繞一個點伸縮  */  ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,                          0.1f, Animation.RELATIVE_TO_SELF, 0.5f,                          Animation.RELATIVE_TO_SELF, 0.5f);                  animationSet.addAnimation(scaleAnimation);                  animationSet.setStartOffset(1000);                  animationSet.setFillAfter(true);                  animationSet.setFillBefore(false);                  animationSet.setDuration(2000);      view.startAnimation(animationSet);   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.