butterfly--1只蝴蝶从左往右振翅起舞

package org.crazyit.image;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class Butterfly extends Activity
{
	//记录蝴蝶ImageView当前的位置
	private float curX = 0;
	private float curY = 30;
	//记录蝴蝶ImageView下一个位置的座标
	float nextX = 0;
	float nextY = 0;	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//获取显示蝴蝶的ImageView组件
		final ImageView imageView = (ImageView)findViewById(R.id.butterfly);	
		final Handler handler = new Handler()
		{
			@Override
			public void handleMessage(Message msg)
			{
				if (msg.what == 0x123)
				{
					//横向上一直向右飞
					if(nextX > 320)
					{
						curX = nextX = 0;
					}
					else
					{
						nextX += 8;
					}
					//纵向上可以随机上下
					nextY = curY + (float)(Math.random() * 10 - 5);
					//设置显示蝴蝶的ImageView发生位移改变
					TranslateAnimation anim 
						= new TranslateAnimation(curX , nextX , curY , nextY);
					curX = nextX;
					curY = nextY;
					anim.setDuration(200);
					//开始位移动画
					imageView.startAnimation(anim);					
				}
			}			
		};
		final AnimationDrawable butterfly = (AnimationDrawable)imageView.getBackground();
		imageView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//开始播放蝴蝶振翅的逐帧动画--加此句蝴蝶就会振翅了
				butterfly.start();
				//通过定制器控制每0.2秒运行一次TranslateAnimation动画
				new Timer().schedule(new TimerTask()
				{
					@Override
					public void run()
					{
						handler.sendEmptyMessage(0x123);						
					}
					
				}, 0, 200);			
			}
		});
	}
}


res/anim/butterfly.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义动画循环播放 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
	android:oneshot="false">
	<item android:drawable="@drawable/butterfly_f01" android:duration="120" />
	<item android:drawable="@drawable/butterfly_f02" android:duration="120" />
	<item android:drawable="@drawable/butterfly_f03" android:duration="120" />
	<item android:drawable="@drawable/butterfly_f04" android:duration="120" />
	<item android:drawable="@drawable/butterfly_f05" android:duration="120" />
	<item android:drawable="@drawable/butterfly_f06" android:duration="120" />				
</animation-list>

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#fff">
	<ImageView
		android:id="@+id/butterfly"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:background="@anim/butterfly"/>
</LinearLayout>



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值