软件编程
位置:首页>> 软件编程>> Android编程>> Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

作者:寒江蓑笠  发布时间:2023-07-30 12:21:29 

标签:android,3D,3D旋转动画,Rotate3dAnimation

利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放。点击开始按钮开始旋转,点击结束按钮停止旋转。

Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

代码如下::

Rotate3dAnimation.java


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初始状态,用于restore()
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
//围绕Y轴旋转degrees度
camera.rotateY(degrees);
//行camera中取出矩阵,赋值给matrix
camera.getMatrix(matrix);
//camera恢复到初始状态,继续用于下次的计算
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

Test3DRotateActivity.java


public class Test3DRotateActivity extends Activity {
/** Called when the activity is first created. */
private final String TAG="Test3DRotateActivity";
private ImageView image;
private Button start ,stop;
private Rotate3dAnimation rotation;
private StartNextRotate startNext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
start=(Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//进行360度的旋转
startRotation(0,360);
}
});
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
image.clearAnimation();
}
});
}
private void startRotation(float start, float end) {
// 计算中心点
final float centerX = image.getWidth() / 2.0f;
final float centerY = image.getHeight() / 2.0f;
Log.d(TAG, "centerX="+centerX+", centerY="+centerY);
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
//final Rotate3dAnimation rotation =new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
//Z轴的缩放为0
rotation =new Rotate3dAnimation(start, end, centerX, centerY, 0f, true);
rotation.setDuration(2000);
rotation.setFillAfter(true);
//rotation.setInterpolator(new AccelerateInterpolator());
//匀速旋转
rotation.setInterpolator(new LinearInterpolator());
//设置监听
startNext = new StartNextRotate();
rotation.setAnimationListener(startNext);
image.startAnimation(rotation);
}
private class StartNextRotate implements AnimationListener{
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.d(TAG, "onAnimationEnd......");
image.startAnimation(rotation);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
}

main.xml


<?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"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/t1"
/>
</LinearLayout>

代码中用Camera来实现动画,Camera就是一个摄像机,一个物体原地不动,我们带着摄像机按设定的角度进行移动,之后从Camera中取出完成该动画的Matrix,然后画我们的物体,这个就是这个3D动画实现的原理。
具体的解释见代码中注释部分,重点说一下Rotate3dAnimation.java中的

matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY);

由于旋转是以(0,0)为中心的,所以为了把界面的中心与(0,0)对齐,就要preTranslate(-centerX, -centerY),旋转完成后,调用postTranslate(centerX, centerY),再把图片移回来,这样看到的动画效果就是activity的界面图片从在centerX为中心绕Y轴旋转了。
你还可以把上面代码改成

matrix.preTranslate(-centerX, 0); matrix.postTranslate(centerX, 0);

看有什么不同效果。

来源:https://blog.csdn.net/heqiangflytosky/article/details/17042977

0
投稿

猜你喜欢

  • 看到有人在问如何实现淘宝商品详情页效果,献上效果图大致梳理一下思路,这里不提供源码状态栏透明使用开源库StatusBarCompat,为了兼
  • 本文为大家分享了java摄像头截图的具体代码,供大家参考,具体内容如下本来sun有个jmf组件可以很方便的实现摄像头截图的,不过这版本后来停
  • 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System
  • 本文实例讲述了C#实现泛型List分组输出元素的方法。分享给大家供大家参考,具体如下:背景:在输出列表时,往往需要按照某一字段进行分组,比如
  • 一、题目描述题目实现:使用套接字实现了服务器端与客户端的通信。运行程序,在服务器端的文本框中输入信息,然后按回车键,客户端就会收到服务器端发
  • 前言一直对它们之间的关系感到好奇,SpringBoot既然是Spring的封装,那么SpringBoot在初始化时应该也会有Bean的加载,
  • 一、注解的概念1、注解官方解释注解叫元数据,一种代码级别的说明,它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举在同一个层次,它可
  • 概述线程池的好处和使用本篇文章就不赘叙了,不了解的可以参考下面两篇文章:一文全貌了解线程池的正确使用姿势学习线程池原理从手写一个线程池开始那
  • 本文实例为大家分享了Android自定义输入法软键盘的具体代码,供大家参考,具体内容如下1 功能描述触屏设备主界面中有一个文本编辑框,底部区
  • 本文实例讲述了java两种单例模式用法。分享给大家供大家参考,具体如下:按照加载方式的不同,单例模式有两种实现:private:只能在同一个
  • c++优先队列(priority_queue)用法详解普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元
  • 简介Exchanger是一个用于线程间数据交换的工具类,它提供一个公共点,在这个公共点,两个线程可以交换彼此的数据。当一个线程调用excha
  • 策略模式的应用场景策略模式是否要使用,取决于业务场景是否符合,有没有必要。是否符合如果业务是处于不同的场景时,采取不同的处理方式的话,就满足
  • 刚学习c#的朋友要自己手动编译c#代码加深记忆,现在总结下如果手动编译!1、先找到系统的.net 环境在一般在 C:\Window
  • SessionSession对象用于获取与数据库的物理连接。 Session对象是重量轻,设计了一个互动是需要与数据库每次被实例化。持久化对
  • 区别1.使用范围和规范不同filter是servlet规范规定的,只能用在web程序中. * 即可以用在web程序中, 也可以用于appli
  • 使用Button时为了让用户有“按下”的效果,有两种实现方式:1.在代码里面。imageButton.setOnTouchListener(
  • 题目要求思路:模拟解题的核心思想在于逆向思维,不考虑每个子数组中的唯一字符个数,转而考虑每个字符可以作为多少个子数组的唯一字符;所以在计算答
  • 今天就是国赛的第一天直接开摆打国赛不如玩羊了个羊玩羊了个羊不如玩MATLAB版写作不易留个赞叭(比赛之余放松一下也行,反正MATLAB版我设
  • 代码编译运行环境:VS2017+Debug+Win32按照参数形式的不同,C++应该有三种函数调用方式:传值调用、引用调用和指针调用。对于基
手机版 软件编程 asp之家 www.aspxhome.com