Android動畫的兩種使用方式。

來源:互聯網
上載者:User

android 動畫的分類就不說了,主要說一下動畫的兩種使用方法:1.通過代碼產生並使用動畫 (不利於重複使用) 2.通過xml檔案產生並使用動畫(更方便,重用性高)。

1.通過代碼產生動畫

//初始化Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);//設定動畫時間scaleAnimation.setDuration(500);                                    this.startAnimation(scaleAnimation);

//初始化Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);//設定動畫時間 translateAnimation.setDuration(1000);this.startAnimation(translateAnimation);

步驟:1.聲明一個動畫 2.設定動畫時間(還可以設定動畫次數等其它屬性) 3.使用動畫。

如果一個控制項要同時使用兩種或兩種以上的動畫,需要通過 AnimationSet(動畫集)來實現。 例如一個imageView同時使用上面兩個動畫,代碼如下:

//初始化 Translate動畫translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);//初始化 Alpha動畫alphaAnimation = new AlphaAnimation(0.1f, 1.0f);//動畫集AnimationSet set = new AnimationSet(true);set.addAnimation(translateAnimation);set.addAnimation(alphaAnimation);//設定動畫時間 (作用到每個動畫)set.setDuration(1000);this.startAnimation(set);
2.通過xml布局檔案產生動畫

ani.xml檔案:

   
上面的xml檔案已經產生了動畫,接下來只要在activity中使用就行了,代碼如下:

Animation animation = AnimationUtils.loadAnimation(this.getContext(), R.anim.ani);
通過上面一行代碼,就能直接使用在xml檔案中的動畫了。



聯繫我們

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