android动画自由落体运动

java源代码:

package com.mobcubes.hai.animation;

import com.mobcubes.hai.R;

import android.R.raw;
import android.app.Activity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 *@Title FreeFallActivity.java
 *@Package com.mobcubes.hai.animation
 *@Description 自由落体运动动画
 *1. 初始位置界面左上角0,0坐标
 *2. 动向下运动,加速
 *3. 到边界弹回,到达高度一半的时候在下落
 *4. 依次类推
 *@Author haiwang
 *@Date 2011-8-18下午01:39:29
 *@Version 1.0
 */
public class FreeFallActivity extends Activity {

	private final String Tag = "FreeFallActivity";
	private ImageView globe;
	private TranslateAnimation tAnimation;//位置变化
	private RotateAnimation rAnimation;
	private float fromX = 0;
	private float fromY = 0;
	private float toX = 0;
	private float toY = 0;
	private int width = 0;//分辨率
	private int height = 0;
	private int count = 6;//运动周期数
	private int n =2;//与count和width得到当前的x坐标
	private int upcount = 1;//向上的次数
	private boolean down = true;//是否是下落
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.freefall);
		globe = (ImageView)this.findViewById(R.id.free_fall_imageview);
		globe.setBackgroundResource(R.drawable.glob);
		getWindowWidthAndHeight();
		toX = width/count;
		height = 410;
		toY = height;
		freefall();
	}
	private void freefall(){
					if(down){//向下
						Log.i(Tag, "down 从("+fromX+" , "+fromY+")到("+toX+" , "+toY+")");
						tAnimation = new TranslateAnimation(fromX, toX, fromY, toY);
						tAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
						tAnimation.setDuration(1000);
						tAnimation.setAnimationListener(new Animation.AnimationListener() {
							
							@Override
							public void onAnimationStart(Animation animation) {
								//Log.i(Tag, "down-start");
							}
							
							@Override
							public void onAnimationRepeat(Animation animation) {
								Log.i(Tag, "down-repeat");
							}
							
							@Override
							public void onAnimationEnd(Animation animation) {
								Log.i(Tag, "down-end");
								if(n>8){
									Log.i(Tag, "over");
									return;
								}
								else{
									freefall();
								}
							}
						});
						rAnimation = new RotateAnimation(0, +360);
						rAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
						rAnimation.setDuration(1000);
						globe.startAnimation(rAnimation);
						globe.startAnimation(tAnimation);
						//设置下次向上的坐标
						down = false;
						fromX = toX;
						fromY = toY;
						//计算这次向上应达到额坐标
						double a = Math.pow(0.5, upcount);
						toY = toY*(float)(1.0-a);
						upcount++;
					}
					else{//向上
						Log.i(Tag, "up 从("+fromX+" , "+fromY+")到("+toX+" , "+toY+")");
						tAnimation = new TranslateAnimation(fromX, toX, fromY, toY);
						tAnimation.setInterpolator(new DecelerateInterpolator());
						tAnimation.setDuration(1000);
						tAnimation.setAnimationListener(new Animation.AnimationListener() {
							
							@Override
							public void onAnimationStart(Animation animation) {
								//Log.i(Tag, "up-start");
							}
							
							@Override
							public void onAnimationRepeat(Animation animation) {
								Log.i(Tag, "up-repeat");
							}
							
							@Override
							public void onAnimationEnd(Animation animation) {
								Log.i(Tag, "up-end");
								if(n>8){
									Log.i(Tag, "over");
									return;
								}
								else{
									freefall();
								}
							}
						});
						rAnimation = new RotateAnimation(0, +360);
						rAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
						rAnimation.setDuration(1000);
						globe.startAnimation(rAnimation);
						globe.startAnimation(tAnimation);
						down = true;
						//设置下次向下的坐标
						fromX = toX;
						fromY = toY;
						toY = height;
					}
					toX = width/count*n;
					n++;
	}
	//得到屏幕的分辨率
	private void getWindowWidthAndHeight(){
		DisplayMetrics displayMetrics = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		width = displayMetrics.widthPixels;
		height = displayMetrics.heightPixels;
		Log.i(Tag, "分辨率为"+width+"X"+height);
	}
}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
  	android:id="@+id/free_fall_imageview"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"/>
</LinearLayout>



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值