实现3D旋转效果的方法
Android中有一种旋转效果,是将一个图片进行360度的旋转。
Matrix的作用是对平面上的View进行缩放、平移、旋转,每一种操作都配了setXXX、preXXX、postXXX三个函数。
Camera不是物理摄像头,是android.graphic下的一个类,相当于手机的屏幕,他的坐标系是带有Z坐标的。
可以完成对指定View的X,Y,Z轴的变化,所以可以用来完成3D效果。但是变化之后并不是直接作用于View,而是修改View的Matrix的值,最终View再根据Matrix的值来变换。
Android APIDemos中已经提供了一种例子
/**
* An animation that rotates the view on the Y axis between two specified angles.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
*/
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;
private Camera mCamera; /**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair
* of X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length
* of the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees the start angle of the 3D rotation
* @param toDegrees the end angle of the 3D rotation
* @param centerX the X center of the 3D rotation
* @param centerY the Y center of the 3D rotation
* @param reverse true if the translation should be reversed, false otherwise
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
} @Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
} @Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore(); matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
如果不需要做特别的效果的话,可以直接照搬
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation; /**
* @author Administrator
*
*/
public class Rotate3DAnimation extends Animation {
// 开始角度
private final float mFromDegrees;
// 结束角度
private final float mToDegrees;
// 中心点
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
// 是否需要扭曲 ,也就是是否有Z轴方向深度的变化
private final boolean mReverse;
// 摄像头
private Camera mCamera;
public Rotate3DAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
} /**
* 生成Transformation,整个动画的过程中会不停的被调用
* @param interpolatedTime 会逐渐从0增大到1
* @param Transformation 变换对象
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
/**
* 实际上整个过程就是通过Camera计算出要旋转所要修改的Matrix的值
* 整个函数结束之后,会根据这个Matrix的值进行变化
* 也就是说实际最终变换依据的依然是Matrix的值
*/
final float fromDegrees = mFromDegrees;
//生成每次的角度
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();//获取动画对象的矩阵
camera.save();//保存摄像机的状态
if (mReverse) { //变化的方向是从小往大变
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
}
//旋转到指定的角度
camera.rotateY(degrees);
//取得变换后的矩阵,修改Matrix的值
camera.getMatrix(matrix);
camera.restore();//恢复摄像机的状态 ,每次摄像机变换完后,下次又从摄像机初始位置开始变化
//将变换的中心点设成中心点
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
Activity中的代码
import org.cxjchen.animation.Rotate3DAnimation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.*;
import android.widget.ImageView;
import android.widget.TextView; /**
* @author Administrator
*
*/
public class Logo_Activity extends Activity { private TextView logo_title = null; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.logo_activity); logo_title = (TextView)findViewById(R.id.logo_title);
logo_title.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
applyRotation(0,360);
}
});
} private void applyRotation(float start, float end) {
// 计算中心点
final float centerX = logo_title.getWidth() / 2.0f;
final float centerY = logo_title.getHeight() / 2.0f;
final Rotate3DAnimation rotation = new Rotate3DAnimation(start, end,
centerX, centerY, 360.0f, false);
rotation.setDuration(1000L);
//设置变换后是否维持变换状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 设置监听
rotation.setAnimationListener(new Animation.AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationRepeat(Animation animation) { } @Override
public void onAnimationEnd(Animation animation) { }
});
logo_title.startAnimation(rotation);
} }
实现3D旋转效果的方法的更多相关文章
- 好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果
原文:好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https:// ...
- css3 3D旋转效果
css3 record2 css3 3D旋转效果 需理解transform css3知识: keyframes transform perspective jsfiddle demo keyframe ...
- 我的世界 ParaCraft 结合开源地图 OpenStreetMap 生成3D校园的方法简介
我的世界ParaCraft结合开源地图OpenStreetMap生成3D校园的方法简介 版本1.0 日期2019.2.3 作者Ray (82735589@qq.com) www.TimeGIS.com ...
- 用ChemDraw画3D图的方法
在绘制化学图形的时候,很多的用户都会发现很多的图形都是三维的,这个时候就需要找一款能够绘制3D图形的化学绘图软件.ChemOffice 15.1是最新的化学绘图工具套件,总共有三个组件,其中ChemD ...
- Css3动画(一) 如何画3D旋转效果或者卫星围绕旋转效果
如何画3D旋转效果或者卫星围绕旋转效果,当然这个也是工作中的一个任务,我在网上翻了一下,并没有找到类似的东西,所以写下来还是费了一番功夫,因此我把它拿出来记录一下,当然替换了一部分内容.好了,话不多说 ...
- C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)
先说实在的:最终解决办法是实现IDocHostUIHandler接口,在GetHostInfo方法里解决,但“实现接口”意味着QueryInterface.AddRef.Release三个方法必须实现 ...
- js 3D旋转效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 基于CSS3的3D旋转效果
自从有了html5和css3,好多以前只能想想的华丽效果都可以上手实现了.3D 转换(个人认为3D变换更贴切^)就是其中之一.关于3D转换,可以阅读CSS3 3D transform变换,不过如此,文 ...
- Unity 3D 基础知识方法
A. 组件中默认的方法有如下: Awake,Start,Update,OnGUI,OnDisable,OnEnable,OnDestory,LateUpdate,FixedUpd ...
随机推荐
- 【Android】Android内存机制,了解Android堆和栈
1.dalvik的Heap和Stack 这里说的只是dalvik java部分的内存,实际上除了dalvik部分,还有native. 下面针对上面列出的数据类型进行说明,只有了解了我们申请的数 ...
- paip.多维理念 输入法的外码输入理论跟文字输出类型精髓
paip.多维理念 输入法的外码输入理论跟文字输出类型精髓 通常,我们的输入法使用的外码是拼音,但是,这个的用户体验很差.. 应该使用多个外码类型... ##按照词汇来源,有如下几个 固有词ati 来 ...
- paip.解决问题Unable to access jarfile E:\resin-4.0.22\lib\resin.jar
paip.解决问题Unable to access jarfile E:\resin-4.0.22\lib\resin.jar 作者Attilax 艾龙, EMAIL:1466519819@qq. ...
- js程序设计02——变量、作用域问题
首先,ECMAScript中的数据类型分为基本类型.引用类型,基本类型的访问操作是按值的.引用类型的值是保存在内存中的对象,操作对象时,实际上操作的是对象的引用,而非对象自身.“javascript高 ...
- JDK版本不兼容问题之:一台机器安装多个版本的JDK
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://guojie.blog.51cto.com/59049/45964 我的机器上最开 ...
- Android系统分区理解及分区目录细解
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- vs2012 MSDN帮助文档离线包下载安装方法
vs2012安装文件 自带的 MSDN帮助文档不全, 需要自己手动添加需要的离线文档包, 具体方法如下 1. 打开 vs2012 2. 按 ctrl + alt + F1 打开帮助文档管理器 3. 在 ...
- Ubuntu虚拟机JeOS安装-2016.08.28
根据官网的说明JeOS的镜像已经在官方不发行了,所以你在别的帖子里看到的安装jeos的镜像地址已经不适用了. 那么应该如何安装这种最小版本的linux操作系统呢? 其一,如果想要安装老版本的jeos系 ...
- Android开发之蓝牙--扫描已经配对的蓝牙设备
一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) 1.3 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
- jsnop
<script src="http://libs.baidu.com/jquery/1.6.1/jquery.min.js"></script> <d ...