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旋转效果的方法的更多相关文章

  1. 好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果

    原文:好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https:// ...

  2. css3 3D旋转效果

    css3 record2 css3 3D旋转效果 需理解transform css3知识: keyframes transform perspective jsfiddle demo keyframe ...

  3. 我的世界 ParaCraft 结合开源地图 OpenStreetMap 生成3D校园的方法简介

    我的世界ParaCraft结合开源地图OpenStreetMap生成3D校园的方法简介 版本1.0 日期2019.2.3 作者Ray (82735589@qq.com) www.TimeGIS.com ...

  4. 用ChemDraw画3D图的方法

    在绘制化学图形的时候,很多的用户都会发现很多的图形都是三维的,这个时候就需要找一款能够绘制3D图形的化学绘图软件.ChemOffice 15.1是最新的化学绘图工具套件,总共有三个组件,其中ChemD ...

  5. Css3动画(一) 如何画3D旋转效果或者卫星围绕旋转效果

    如何画3D旋转效果或者卫星围绕旋转效果,当然这个也是工作中的一个任务,我在网上翻了一下,并没有找到类似的东西,所以写下来还是费了一番功夫,因此我把它拿出来记录一下,当然替换了一部分内容.好了,话不多说 ...

  6. C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)

    先说实在的:最终解决办法是实现IDocHostUIHandler接口,在GetHostInfo方法里解决,但“实现接口”意味着QueryInterface.AddRef.Release三个方法必须实现 ...

  7. js 3D旋转效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 基于CSS3的3D旋转效果

    自从有了html5和css3,好多以前只能想想的华丽效果都可以上手实现了.3D 转换(个人认为3D变换更贴切^)就是其中之一.关于3D转换,可以阅读CSS3 3D transform变换,不过如此,文 ...

  9. Unity 3D 基础知识方法

    A. 组件中默认的方法有如下:            Awake,Start,Update,OnGUI,OnDisable,OnEnable,OnDestory,LateUpdate,FixedUpd ...

随机推荐

  1. 白条VS花呗,快餐式消费金融成巨头新战场

    在这一次的国庆假期前,90后网红密子君吃空麦当劳事件引发了网友们的热议.短短半个小时,这位90后网红就吃光了25包薯条,随后又吃下两杯麦旋风,其疯狂举动引得四周食客纷纷围观拍照.那么,是什么刺激这位9 ...

  2. C++11中async中future用法(一)

    async意味着异步执行代码,看如下示例: #include <future> #include <thread> #include <chrono> #inclu ...

  3. web前端基础——初识HTML DOM编程

    1 HTML DOM编程概述 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理HTML的标准编程接口.由于HTML文档被浏览器解析后就是一棵DOM树,要改 ...

  4. linux下IPTABLES配置详解(转)

    如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...

  5. android: SQLite更新数据

    学习完了如何向表中添加数据,接下来我们看看怎样才能修改表中已有的数据. SQLiteDatabase 中也是提供了一个非常好用的 update()方法用于对数据进行更新,这个方法 接收四个参数,第一个 ...

  6. 使用HTML5的File实现base64和图片的互转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Android调用浏览器打开网址遇到的问题

    我自己的手机(一加一代,升级了氢OS),然后在点击游戏内一个"隐私政策"-- 需要打开一个网页,然后就crash了.出错的信息如下: 完全是看不出来,然后我单独写了一个demo来测 ...

  8. Windows Server 2008 R2 备份和恢复 (转)

    Windows Server Backup : 1.安装Windows Server Backup的方法: 通过"服务器管理器"中的"添加功能"向导进行安装. ...

  9. Number Range 管理之并行缓冲

    Number Range 管理之并行缓冲: 常用的事务代码SNRO,SM56还有一些业务专用的号码管理,可以在SPRO中查找: SNRO :Number Range 管理 一般的操作是维护号码范围.如 ...

  10. (转)TRANSFORM_TEX详解

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 特别讲:常见问题回答   本讲会陆续补充一些问题的解答. 问: (1) TRANSFORM_TEX是做什么的 (2)float4 _Main ...