原文地址:http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847

其实网上的3D旋转的例子很多,在这里我只是想把其代码做一个解释。

先上图:

代码:

TurnAroundActivity

/**
* 图片浏览器的主Activity。
*
* @author guolin
*/
public class TurnAroundActivity extends Activity { /**
* 根布局
*/
private RelativeLayout layout; /**
* 用于展示图片列表的ListView
*/
private ListView picListView; /**
* 翻转后的布局
*/
private LinearLayout picture; /**
* 图片列表的适配器
*/
private PictureAdapter adapter; /**
* 存放所有图片的集合
*/
private List<Picture> picList = new ArrayList<Picture>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_around);
// 对图片列表数据进行初始化操作
initPics();
layout = (RelativeLayout) findViewById(R.id.layout);
picListView = (ListView) findViewById(R.id.pic_list_view);
picture = (LinearLayout) findViewById(R.id.picture);
adapter = new PictureAdapter(this, 0, picList);
picListView.setAdapter(adapter);
picListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 获取布局的中心点位置,作为旋转的中心点
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 构建3D旋转动画对象,旋转角度为0到90度,这使得ListView将会从可见变为不可见
final Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,
310.0f, true);
// 动画持续时间500毫秒
rotation.setDuration(500);
// 动画完成后保持完成的状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 设置动画的监听器
rotation.setAnimationListener(new TurnToImageView());
layout.startAnimation(rotation);
}
});
picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 获取布局的中心点位置,作为旋转的中心点
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 构建3D旋转动画对象,旋转角度为360到270度,这使得ImageView将会从可见变为不可见,并且旋转的方向是相反的
final Rotate3dAnimation rotation = new Rotate3dAnimation(360, 270, centerX,
centerY, 310.0f, true);
// 动画持续时间500毫秒
rotation.setDuration(500);
// 动画完成后保持完成的状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 设置动画的监听器
rotation.setAnimationListener(new TurnToListView());
layout.startAnimation(rotation);
}
});
} /**
* 初始化图片列表数据。
*/
private void initPics() {
Picture bird = new Picture("Bird", R.drawable.bird);
picList.add(bird);
Picture winter = new Picture("Winter", R.drawable.winter);
picList.add(winter);
Picture autumn = new Picture("Autumn", R.drawable.autumn);
picList.add(autumn);
Picture greatWall = new Picture("Great Wall", R.drawable.great_wall);
picList.add(greatWall);
Picture waterFall = new Picture("Water Fall", R.drawable.water_fall);
picList.add(waterFall);
} /**
* 注册在ListView点击动画中的动画监听器,用于完成ListView的后续动画。
*
* @author guolin
*/
class TurnToImageView implements AnimationListener { @Override
public void onAnimationStart(Animation animation) {
} /**
* 当ListView的动画完成后,还需要再启动ImageView的动画,让ImageView从不可见变为可见
*/
@Override
public void onAnimationEnd(Animation animation) {
// 获取布局的中心点位置,作为旋转的中心点
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 将ListView隐藏
picListView.setVisibility(View.GONE);
// 将ImageView显示
picture.setVisibility(View.VISIBLE);
picture.requestFocus();
// 构建3D旋转动画对象,旋转角度为270到360度,这使得ImageView将会从不可见变为可见
final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,
310.0f, false);
// 动画持续时间500毫秒
rotation.setDuration(500);
// 动画完成后保持完成的状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
layout.startAnimation(rotation);
} @Override
public void onAnimationRepeat(Animation animation) {
} } /**
* 注册在ImageView点击动画中的动画监听器,用于完成ImageView的后续动画。
*
* @author guolin
*/
class TurnToListView implements AnimationListener { @Override
public void onAnimationStart(Animation animation) {
} /**
* 当ImageView的动画完成后,还需要再启动ListView的动画,让ListView从不可见变为可见
*/
@Override
public void onAnimationEnd(Animation animation) {
// 获取布局的中心点位置,作为旋转的中心点
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 将ImageView隐藏
picture.setVisibility(View.GONE);
// 将ListView显示
picListView.setVisibility(View.VISIBLE);
picListView.requestFocus();
// 构建3D旋转动画对象,旋转角度为90到0度,这使得ListView将会从不可见变为可见,从而回到原点
final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
310.0f, false);
// 动画持续时间500毫秒
rotation.setDuration(500);
// 动画完成后保持完成的状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
layout.startAnimation(rotation);
} @Override
public void onAnimationRepeat(Animation animation) {
} } }

关键类:

Rotate3dAnimation
/**
* 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();
//z轴上的景深
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();
//camera.rotateY(degrees);其实围绕y轴旋转的坐标点是在(0,0);
//为了让其在围绕(centerX, centerY)点
//preTranslate 作用为在rotateY开始之前先把坐标(centerX, centerY)移到中心点
matrix.preTranslate(-centerX, -centerY);
//postTranslate 当执行完rotateY后再把中心点移动回来
matrix.postTranslate(centerX, centerY);
}
}

layout文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <ListView
android:id="@+id/pic_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView> <LinearLayout
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:visibility="gone"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="ddddddd"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/bird"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ddddddd"/> </LinearLayout> </RelativeLayout>

android3D动画,绕y轴旋转的更多相关文章

  1. three.js 对象绕任意轴旋转--模拟门转动

    说了几篇的数学方法,这篇放松一下,郭先生说说绕任意轴转动.说一说其中一种方法,也是比较容易理解的一种,它的原理就是将子对象放到一个盒子中,然后改变子对象相对于父对象的位置(因为子对象的原点默认还是在盒 ...

  2. CSS3 动画3D视角下 旋转圆环

    首先是  transform 属性: transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 加上对应属性则可得到3D透视效果下的形态 本次以圆形( ...

  3. X、Y轴抖动的动画

    实现这个动画效果用到了interpolator属性,这样就能让一些控件产生自定义的抖动效果 这是用作interpolator的文件,用来做动画循环 cycle.xml <?xml version ...

  4. 如何让一个sprite绕一个点旋转,同时又可以实现指定旋转角度并慢慢停下的效果

    如何让一个sprite绕一个点旋转,同时又可以实现指定旋转角度并慢慢停下的效果 首先列出sprite围绕一个点旋转的公式,这个可以自己推导,假设sprite的起始位置为(x1,y1),围绕旋转的中心点 ...

  5. JavaScript求两点之间相对于Y轴的顺时针旋转角度

    需求: 已知一个向量,初始位置在y轴方向,如图红色箭头,绕中心点(x1, y1)旋转若干角度后,到达Line(x2,y2 x1,y1)的位置,求旋转角度 分析: 坐标点(x1, y1)(x2, y2) ...

  6. chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法[bubuko.com]

    chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法,原文:http://bubuko.com/infodetail-328671.html 默认情况下如下图 Y轴并不是从0开始 ...

  7. 3d照片环效果(修改版--添加了x轴y轴双向转动和修复模糊度的bug)

    今天用用前两天总结的css3新效果写了一个3d照片环的效果,其中还有些bug大家可以看一看,一起改进. <!DOCTYPE html> <html lang="en&quo ...

  8. echarts使用笔记四:双Y轴

    1.双Y轴显示数量和占比 app.title = '坐标轴刻度与标签对齐'; option = { title : { //标题 x : 'center', y : 5, text : '数量和占比图 ...

  9. fusioncharts Y轴不显示中文的解决方法(转载)

    使用fusionChart主要是被其界面吸引了,各类图表都很好看,下载以后文档也很周全,支持的语言也很多种 ,容易上手.fusionChart工作原理主要是通过后台传xml数据源给报表前台flash ...

随机推荐

  1. 解读ASP.NET 5 & MVC6系列(9):日志框架

    框架介绍 在之前的.NET中,微软还没有提供过像样的日志框架,目前能用的一些框架比如Log4Net.NLog.CommonLogging使用起来多多少少都有些费劲,和java的SLF4J根本无法相比. ...

  2. ConcurrentHashMap

    ConcurrentHashMap是Java5中新增加的一个线程安全的Map集合,可以用来替代HashTable.对于ConcurrentHashMap是如何提高其效率的,可能大多人只是知道它使用了多 ...

  3. 关于GIT合并出错的记录

    今天给美术解决GIT资源冲突时碰到的问题,搞了挺长时间终于解决了.参看下面这个网址:http://www.bujichong.com/m/68 今天git一小部分代码发现出错了, 上网查了一下, 大体 ...

  4. CSS3常用属性(边框、背景、文本效果、2D转换、3D转换、过渡、有过渡效果大图轮播、动画)

    CSS3边框: 1.CSS3圆角:border-radius  属性--创建边框线的圆角 <body style="font-size:24px; color:#60F;"& ...

  5. 以 Console 方式运行、调试、编译 .Net 编写的 Windows 服务

    经常看到一些人在调试 Windows 服务时,很执著的在附加进程后调试!其实 .Net 编写的 Windows 应用程序,包括 Windows 服务都可以编译成 Console 程序!甚至于 ASP. ...

  6. php 设计模式--准备篇

    要了解设计模式 首先我们要先了解 php的命名空间和类的自动载入的功能 下面我们来说一下 命名空间 概念缘由:比如一个a.php的文章 但是我们需要两个 此时同一个目录下不可能存在两个a.php 那么 ...

  7. 1.ios synthesize有什么作用

    ###1.ios synthesize有什么作用 当定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,所以Xcode提供了@property和@synthe ...

  8. JavaScript零基础学习系列五

    定时器 1.定时器:设定时间,在指定的时间之后执行函数或者是程序   a.反复性定时器:var dingshiqi=Window.setInterval("函数名()",时间n[毫 ...

  9. Maven命令

    1. mvn help:describe 你是否因为记不清某个插件有哪些goal而痛苦过,你是否因为想不起某个goal有哪些参数而苦恼,那就试试这个命令吧,它会告诉你一切的. 参数: 1. -Dplu ...

  10. Java的修饰符

    转自:http://blog.csdn.net/manyizilin/article/details/51926230#L42 修饰符: 像其他语言一样,Java可以使用修饰符来修饰类中方法和属性.主 ...