一个不甚完美的多功能三维动画的类

在andorid中,使用camera,matrix和动画Animation类,结合activity, view, window 可以创建出令人目眩神迷的场景动画,为锁屏,启动器,动态墙纸提供丰富的视觉效果。下面的动画类可以创建以Y轴为中心旋转,沿X, Y, X三个方向移动的动画,当然沿Y轴移动的动画也能变相的实现VIEW缩小放大功能。几种效果可以同时具有,这时会产生像降落伞或者直升机一样沿垂直和水平方向加旋转缩小的动画效果,也可以做为在重力和水平风力的效果。这个类经过完善,完全可以再添加上沿X,Z轴旋转的效果,再加上侦听效果。不过一个根据个人需要修改吧,大而全的功能,往往意味需要增加更多的CPU运算时间或者内存开销。

package com.example.scr;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Rotate3dAnimation extends Animation {
	// 开始角度
	private final float mFromDegrees;
	// 结束角度
	private final float mToDegrees;
	// 开始x
	private final float mFromx;
	// 结束x
	private final float mTox;
	// 开始y
	private final float mFromy;
	// 结束y
	private final float mToy;
	// 开始z
	private final float mFromz;
	// 结束z
	private final float mToz;
	// 中心点
	private final float mCenterX;
	private final float mCenterY;
	// 是否需要返回
	private final boolean mReverse;
	// 摄像头
	private Camera mCamera;
	
	private Runnable mExitFunc = null;

	public void setExitFunction(Runnable run) {
		mExitFunc = run;
	}


	public Rotate3dAnimation(float fromDegrees, float toDegrees, 
			float fromx, float tox,float fromy, 
			float toy,float fromz,float toz,float centerX,
			float centerY, boolean reverse) {
		mFromDegrees = fromDegrees;
		mToDegrees = toDegrees;
		mFromx = fromx;
		mTox = tox;
		mFromy = fromy;
		mToy = toy;
		mFromz = fromz;
		mToz = toz;
		mCenterX = centerX;
		mCenterY = centerY;
		mReverse = reverse;
	}

	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
		mCamera = new Camera();
	}

	// 生成Transformation
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		final float fromDegrees = mFromDegrees;
		// 生成中间角度
		float degrees = mFromDegrees
				+ ((mToDegrees - mFromDegrees) * interpolatedTime);
		float X = mFromx
				+ ((mTox - mFromx) * interpolatedTime);
		float Y = mFromy
				+ ((mToy - mFromy) * interpolatedTime);
		float Z = mFromz
				+ ((mToz - mFromz) * interpolatedTime);
		final float centerX = mCenterX;
		final float centerY = mCenterY;
		final Camera camera = mCamera;
		final Matrix matrix = t.getMatrix();
		camera.save();
		if (mReverse) {
			camera.translate(X*interpolatedTime, Y* interpolatedTime, Z * interpolatedTime);
		} else {
			camera.translate(X*(1-interpolatedTime), Y* (1-interpolatedTime), Z * (1.0f - interpolatedTime));
		}
		camera.rotateY(degrees);
		// 取得变换后的矩阵
		camera.getMatrix(matrix);
		camera.restore();
		matrix.preTranslate(-centerX, -centerY);
		matrix.postTranslate(centerX, centerY);
	}

	@Override
	public boolean getTransformation(long currentTime,
			Transformation outTransformation) {
		// TODO Auto-generated method stub
		return super.getTransformation(currentTime, outTransformation);
	}
  
}

在使用上也十分简单,这里可以看做是个飞走的飞机。

private void applyRotation(float start, float end) {
		// 计算中心点
		final float centerX = Ulv.getWidth() / 2.0f;
		final float centerY = Ulv.getHeight() / 2.0f;
		final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, 0,
				1000, 0, 1000, 0, 1000, centerX, centerY, true);
		rotation.setDuration(1000);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		// 设置监听
		rotation.setAnimationListener(new Animation.AnimationListener() {

			public void onAnimationStart(Animation animation) {
			}

			// 动画结束
			public void onAnimationEnd(Animation animation) {
				if (mExitFunc != null) {
					mExitFunc.run();
				}
			}

			public void onAnimationRepeat(Animation animation) {
			}
		});

		Pmv.startAnimation(rotation);
		Ulv.startAnimation(rotation);
	}


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Asuo二维绘图控件v2.0》=============================================================================软件名称:《Asuo二维绘图控件》软件版本: V2.0软件作者:asuo作者邮箱:asuo@263.net软件网站:http://www.asuo.com下载地址:http://www.asuo.com/down/as2d.rar 下载软件语言:vc++授权形式:(ActiveX控件)应用平台:win9x/me/2000/xp界面预览:http://www.asuo.com/as2d_active.htm软件简介:平面图形控件,可以创建和编辑矢量图形以及便捷的显示操作。 文字和尺寸标注;元素属性设置;长度、角度、面积的查询; DXF输出;提供交换文件和二次开发接口。适用于 CAD/CAM/GIS/GPS等相关图形软件开发。软件价格:试用版本免费相关文档:--发布日期:2003.07.29============================================================================== 1.库文件清单: MFC42.DLL MSVCIRT.DLL MSVCRT.DLL ASBASE.DLL ASCSTR.DLL ASDRAW.DLL ASINTR.DLL ASKERN.DLL ASLAW.DLL ASOCX.OCX ASSPLINE.DLL2.控件注册: 控件注册,命令为: regsvr32 Asocx.ocx 解除注册: regsvr32 Asocx.ocx /u3.功能说明:选择:单击鼠标左键选择元素; 框选:在选择状态下,按住鼠标托动进入框选状态; 选择移动元素:在选择状态下,如果当前捕捉到元素,按住鼠标托动元素; 重画(REDRAW):刷新绘图区域; 显示平移(PAN):移动视点,调整看图位置; 框选放大(ZOOM):选择一个放大区域,需要点两点; 显示放大缩小:显示放大为1.2倍,显示缩小为0.8倍; 创建点:选点创建; 创建直线:两点创建直线; 圆心+圆上一点:需要两点创建; 两点圆:需要两点,此两点为圆上点且它们之间距离为直径; 3点圆:需三点,创建过三点的圆,三点共线时创建失败; 圆弧:圆心+起点+终点,需要三点,第一点为圆心,第二点为弧的起点,第三点为弧的终点,弧以逆时针方向; 半圆弧:似两点圆,逆时针方向; 三点弧:需要三点,创建出过此三点的圆弧;逆时针方向; 椭圆:先给出长轴,然后给椭圆上一点; 样条:依次给出样条线的控制点,双击鼠标左键结束创建样条,样条线必须有三个以上的控制点; 矩形:给出矩形对角两点创建矩形; 删除:删除当前选择到的元素; 复制:复制当前选择到的元素; ...4.开发帮助: 如果用户需要进行特殊的功能扩充,请参考《用户开发手册 index.htm》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值