Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Animation 動畫完全指南

編輯:關於 Android 程式設計

Android 的 Animation 主要分兩類:一是漸變動畫(tweened animation),二是逐幀動畫(frame-by-frame animation)。漸變動畫用一幀圖片就能產生四種效果——alpha(透明度)、rotate(旋轉)、scale(縮放)、translate(平移),可通過代碼或 xml 兩種方式實現;逐幀動畫則由一系列圖片快速播放達到動的效果。此外還會穿插講解 AnimationSet、AnimationListener、LayoutAnimationController。

實例一:代碼實現 tweened animation

① 新建布局文件:四個 Button 加一個 ImageView。

    

② Activity:

package com.example.f_animation_tween; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends ActionBarActivity { private Button button_alpha, button_rotate, button_scale, button_trans; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_alpha = (Button) findViewById(R.id.button_alpha); button_rotate = (Button) findViewById(R.id.button_rotate); button_scale = (Button) findViewById(R.id.button_scale); button_trans = (Button) findViewById(R.id.button_translate); imageView = (ImageView) findViewById(R.id.imageview); button_alpha.setOnClickListener(new ButtonAlphaListerner()); button_rotate.setOnClickListener(new ButtonRotateListerner()); button_scale.setOnClickListener(new ButtonScaleListerner()); button_trans.setOnClickListener(new ButtonTransListerner()); } private class ButtonAlphaListerner implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 新建一個AlphaAnimation,構造器參數代表從1到0的淡化 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); // 淡化時長為2秒 alphaAnimation.setDuration(2000); // 重復兩次,加一次是三次 alphaAnimation.setRepeatCount(2); // 設置一個監聽器 alphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation arg0) { System.out.println("--->start"); } @Override public void onAnimationRepeat(Animation arg0) { System.out.println("--->repeat"); } @Override public void onAnimationEnd(Animation arg0) { System.out.println("--->end"); } }); // 讓圖片產生淡化動畫 imageView.startAnimation(alphaAnimation); // 也可以讓其他控件產生動畫 button_alpha.startAnimation(alphaAnimation); } } private class ButtonRotateListerner implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 旋轉動畫,圍繞的中心點由構造器得出: // 第三四個參數代表X軸相對於自身,五六個參數Y軸同理,相當於圖片的 // 右下角的點,Animation.RELATIVE_TO_PARENT是相對於父控件 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 1); rotateAnimation.setDuration(2000); imageView.startAnimation(rotateAnimation); } } private class ButtonScaleListerner implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 橫向縱向都縮小為原來的0.1,圍繞的點和上面同理 ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 1); scaleAnimation.setDuration(2000); // 讓其定格在最後 scaleAnimation.setFillAfter(true); imageView.startAnimation(scaleAnimation); } } private class ButtonTransListerner implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0); AnimationSet animationSet = new AnimationSet(true); // 可以通過一個AnimationSet添加多個動畫效果,他是Animation的子類 animationSet.addAnimation(translateAnimation); animationSet.addAnimation(alphaAnimation); animationSet.setDuration(2000); imageView.startAnimation(animationSet); } } }

實例二:用 XML 實現漸變動畫

① 在 res 下新建 anim 文件夾,添加一個 alpha.xml:

  

② Activity 主要代碼:

private class ButtonAlphaListerner implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Animation alphaAnimation = AnimationUtils.loadAnimation( MainActivity.this, R.anim.alpha); imageView.startAnimation(alphaAnimation); } }

對於批量修改,xml 方式更快;也可以在 xml 裡組合多個動畫效果,具體參數上網查即可。

實例三:逐幀動畫

① 在 drawable 下放入幾張圖片,新建一個 anim.xml:

    

② Activity 主要代碼:

private class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setBackgroundResource(R.drawable.anim); AnimationDrawable animationDrawable = (AnimationDrawable) imageView .getBackground(); //AnimationDrawable類就是用於實現逐幀動畫 isStart = !isStart; if (isStart) animationDrawable.start(); else animationDrawable.stop(); } }

實例四:LayoutAnimationController 的使用

A layout animation controller is used to animate a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time.

ListView 也繼承自 ViewGroup,就以它為例做個 Demo。

① 布局文件:

    

② 在 res 下新建 anim 文件夾,新建 anim.xml 和 anim_layout.xml 兩個文件:

  
 

以上 xml 分別設置動畫效果、執行順序與間隔時間。

③ Activity:

package com.example.f_animation_layoutcon; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.LayoutAnimationController; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class MainActivity extends ActionBarActivity { private Button button; private ListView listView; private ArrayAdapter adapter; private List list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); listView = (ListView) findViewById(R.id.listview); list = new ArrayList(); list.add("北京"); list.add("上海"); list.add("廣州"); adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Animation animation = AnimationUtils.loadAnimation( MainActivity.this, R.anim.anim); LayoutAnimationController lac = new LayoutAnimationController( animation); lac.setOrder(LayoutAnimationController.ORDER_RANDOM); lac.setDelay(1.0f); listView.setLayoutAnimation(lac); listView.setAdapter(adapter); // 也可以直接在listview裡面指定layoutAnimation // 在ListView中添加屬性android:layoutAnimation="@anim/anim_layout" } }); } }

執行效果:點擊 Button 後,ListView 的項目一個一個顯示,而不是一下全部出現。

小結:1、tween animation 有代碼與 XML 兩種實現,XML 更利於批量修改,配合事件監聽器使用;2、frame-by-frame animation;3、LayoutAnimationController 同樣有兩種實現方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved