Android Animation之frame animation

Frame animation由一系列的静态图片构成,并且按照一定的顺序连续显示,类似电影的感觉。


使用animation-list创建图片的序列,属性android:oneshot表示是否循环播放。每个图片使用item标签包含在animation-list中,item包括android:drawable和android:duration属性,分别表示图片资源的id和每张图片的显示时间。

Frame代码如下

<?xml version="1.0" encoding="utf-8"?>
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >
    
    <item android:drawable="@drawable/number_0" android:duration="600" />
    <item android:drawable="@drawable/number_1" android:duration="600" />
    <item android:drawable="@drawable/number_2" android:duration="600" />
    <item android:drawable="@drawable/number_3" android:duration="600" />
    <item android:drawable="@drawable/number_4" android:duration="600" />
    <item android:drawable="@drawable/number_5" android:duration="600" />
    <item android:drawable="@drawable/number_6" android:duration="600" />
    <item android:drawable="@drawable/number_7" android:duration="600" />
    <item android:drawable="@drawable/number_8" android:duration="600" />
    <item android:drawable="@drawable/number_9" android:duration="600" />

</animation-list>

Activity代码如下

package com.none.funcynumber;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class FancyActivity extends Activity {

	private AnimationDrawable jumpAnim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final ImageView number = (ImageView) findViewById(R.id.number);
		final AnimationDrawable animNumber = (AnimationDrawable) number.getDrawable();
		number.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				animNumber.start();
			}
		});
		
		Button incr = (Button) findViewById(R.id.incr);
		incr.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AnimationDrawable incrAnim = (AnimationDrawable) getResources().getDrawable(R.anim.increase);
				number.setImageDrawable(incrAnim);
				incrAnim.start();
			}
		});
		
		Button decr = (Button) findViewById(R.id.decr);
		decr.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AnimationDrawable decrAnim = (AnimationDrawable) getResources().getDrawable(R.anim.decrease);
				number.setImageDrawable(decrAnim);
				decrAnim.start();
			}
		});
		
		jumpAnim = subAnimation(animNumber, 0, 10);
		Drawable standingStill = 
				getResources() 
				.getDrawable(R.drawable.number_0);
		jumpAnim.addFrame(standingStill, 600);
		jumpAnim.setOneShot(true);
		
		Button jump = (Button) findViewById(R.id.jump);
		jump.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				number.setImageDrawable(jumpAnim);
				jumpAnim.start();
				jumpAnim.setVisible(true, true);
				jumpAnim.setAlpha(0x80);
			}
		});
	}
	
	private AnimationDrawable subAnimation(AnimationDrawable src, int start,
			int end) {
		AnimationDrawable subAnim = new AnimationDrawable();
		subAnim.setOneShot(src.isOneShot());
		for (int i = start; i < end; i++) {
			int index = (int) (Math.random() * 1.5 * i);
			subAnim.addFrame(src.getFrame(index), src.getDuration(index));
		}
		return subAnim;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.fancy, menu);
		return true;
	}

}


你可能感兴趣的:(Android Animation之frame animation)