Android Animation 動畫Demo

來源:互聯網
上載者:User

本文主要介紹Android中的Animation動畫。

Android提供了2中動畫:Tween動畫和Frame動畫。

本文中主要講解Tween動畫,下篇文章中會講到Frame動畫。

Tween動畫:

通過對View的內容進行一系列的圖形變換(包括平移,縮放,旋轉,改變透明度)來實現動畫的效果,動畫效果的定義可以採用XML方式也可以採用編碼來做Tween動畫(文章最後會給出兩種方式動畫的原始碼Demo)。

動畫的類型

Xml定義動畫使用的配置節點

編碼定義動畫使用的類

漸層透明度動畫效果(簡稱透明動畫)

AlphaAnimation

漸層尺寸縮放動畫效果(縮放動畫)

ScaleAnimation

畫面位置移動動畫效果(移位動畫)

TranslateAnimation

畫面旋轉動畫效果(旋轉動畫)

RotateAnimation


實現:

原始碼:

:

布局檔案activity_main:

                                

anim檔案夾下的動畫布局:

相應的動畫屬性並沒有詳細標示,讀者可自行研究,爭取達到隨心所欲修改動畫效果的目的。

透明動畫aa.xml檔案:



旋轉動畫ra.xml:



縮放動畫sa.xml:



移位動畫ta.xml:



透明動畫+移位動畫的xml檔案,set1.xml:

        

旋轉動畫+縮放動畫的xml檔案,set2.xml:

        

MainActivity代碼:

package com.myanimationdemo2;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AnimationUtils;import android.widget.Button;import com.myanimationdemo2.R.anim;public class MainActivity extends Activity implements OnClickListener {private Button button_alphaAnim, button_rotateAnim, button_translateAnim,button_scaleAnim, button_setAnim1, button_setAnim2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button_alphaAnim = (Button) findViewById(R.id.button_alphaAnim);button_rotateAnim = (Button) findViewById(R.id.button_rotateAnim);button_translateAnim = (Button) findViewById(R.id.button_translateAnim);button_scaleAnim = (Button) findViewById(R.id.button_scaleAnim);button_setAnim1 = (Button) findViewById(R.id.button_setAnim1);button_setAnim2 = (Button) findViewById(R.id.button_setAnim2);button_alphaAnim.setOnClickListener(this);button_rotateAnim.setOnClickListener(this);button_translateAnim.setOnClickListener(this);button_scaleAnim.setOnClickListener(this);button_setAnim1.setOnClickListener(this);button_setAnim2.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {/** * 透明動畫 */case R.id.button_alphaAnim:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.aa));break;/** * 旋轉動畫 */case R.id.button_rotateAnim:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.ra));break;/** * 移位動畫 */case R.id.button_translateAnim:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.ta));break;/** * 縮放動畫 */case R.id.button_scaleAnim:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.sa));break;/** * 透明動畫+移位動畫 */case R.id.button_setAnim1:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.set1));break;/** * 旋轉動畫+縮放動畫 */case R.id.button_setAnim2:v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), anim.set2));break;default:break;}}}

以上為Tween動畫中,以xml形式給出的動畫Demo代碼,對直接在代碼中實現動畫Demo並沒有做詳細介紹。

下面給出兩者的原始碼Demo:

以xml形式的動畫Demo原始碼:

點擊下載源碼

直接在代碼中編寫的Demo原始碼:

點擊下載源碼








聯繫我們

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