Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:Animation動畫

Android:Animation動畫

編輯:關於Android編程

Animation主要分為兩類:一類是漸變動畫tweened animation,一類是逐幀動畫frame-by-frame animation。漸變動畫就是用一幀圖片產生四種效果:1、alpha,2、rotate,3、scale,4、translates,可以通過代碼或者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) {
					// TODO Auto-generated method stub
					System.out.println("--->start");
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {
					// TODO Auto-generated method stub
					System.out.println("--->repeat");
				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					// TODO Auto-generated method stub
					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文件




    
    
    
    
a
②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 animated 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對批量修改更有用,事件監聽器。2、frame-by-frame animation。3、LayoutAnimationController也是有兩種實現方式。















  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved