@@ -211,18 +211,311 @@ void rotateY (float deg);
211211void rotateZ (float deg);
212212```
213213
214- 这个东西说理论也不好理解 ,直接上图:
214+ 这个东西瞎扯理论也不好理解 ,直接上图:
215215
216216<p align =" center " >
217217<img src =" http://ww4.sinaimg.cn/large/005Xtdi2jw1f7sg375gbgg308c0eak4l.gif " width =" 240 " />
218218<img src =" http://ww2.sinaimg.cn/large/005Xtdi2jw1f7sgtl0nryg308c0eatm8.gif " width =" 240 " />
219219<img src =" http://ww4.sinaimg.cn/large/005Xtdi2jw1f7sgtxp6awg308c0eah5h.gif " width =" 240 " />
220220</p >
221221
222+ ** 以上三张图分别为,绕x轴,y轴,z轴旋转的情况,至于为什么没有显示z轴,是因为z轴是垂直于手机屏幕的,在屏幕上的投影就是一个点。**
222223
224+ 关于旋转,有以下几点需要注意:
223225
226+ #### 默认旋转中心
224227
225- ## 核心要点
228+ 旋转中心默认是坐标原点,对于图片来说就是左上角位置。
229+
230+ #### 如何控制旋转中心
231+
232+ 我们都知道,在2D中,不论是旋转,错切还是缩放都是能够指定操作中心点位置的,但是在3D中却没有默认的方法,如果我们想要让图片围绕中心点旋转怎么办? 这就要使用到我们在[ Matrix原理] ( http://www.gcssloop.com/customview/Matrix_Basic ) 提到过的方法,虽然当时因为有更好的选择方案,并不提倡这样做:
233+
234+ ``` java
235+ Matrix temp = new Matrix (); // 临时Matrix变量
236+ this . getMatrix(temp); // 获取Matrix
237+ temp. preTranslate(- centerX, - centerY); // 使用pre将旋转中心移动到和Camera位置相同。
238+ temp. postTranslate(centerX, centerY); // 使用post将图片(View)移动到原来的位置
239+ ```
240+
241+ #### 官方示例
242+
243+ 说到3D旋转,最经典的应该就是ApiDemo里面的 [ Rotate3dAnimation] ( https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/animation/Rotate3dAnimation.java ) 了,见过不少博文都是根据Rotate3dAnimation修改的效果,这是一个非常经典的例子,鉴于代码也不长,就贴在这里和大家一起品鉴一下。
244+
245+ ``` java
246+ public class Rotate3dAnimation extends Animation {
247+ private final float mFromDegrees;
248+ private final float mToDegrees;
249+ private final float mCenterX;
250+ private final float mCenterY;
251+ private final float mDepthZ;
252+ private final boolean mReverse;
253+ private Camera mCamera;
254+ /**
255+ * 创建一个绕y轴旋转的3D动画效果,旋转过程中具有深度调节,可以指定旋转中心。
256+ *
257+ * @param fromDegrees 起始时角度
258+ * @param toDegrees 结束时角度
259+ * @param centerX 旋转中心x坐标
260+ * @param centerY 旋转中心y坐标
261+ * @param depthZ 最远到达的z轴坐标
262+ * @param reverse true 表示由从0到depthZ,false相反
263+ */
264+ public Rotate3dAnimation (float fromDegrees , float toDegrees ,
265+ float centerX , float centerY , float depthZ , boolean reverse ) {
266+ mFromDegrees = fromDegrees;
267+ mToDegrees = toDegrees;
268+ mCenterX = centerX;
269+ mCenterY = centerY;
270+ mDepthZ = depthZ;
271+ mReverse = reverse;
272+ }
273+ @Override
274+ public void initialize (int width , int height , int parentWidth , int parentHeight ) {
275+ super . initialize(width, height, parentWidth, parentHeight);
276+ mCamera = new Camera ();
277+ }
278+ @Override
279+ protected void applyTransformation (float interpolatedTime , Transformation t ) {
280+ final float fromDegrees = mFromDegrees;
281+ float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
282+ final float centerX = mCenterX;
283+ final float centerY = mCenterY;
284+ final Camera camera = mCamera;
285+ final Matrix matrix = t. getMatrix();
286+ camera. save();
287+
288+ // 调节深度
289+ if (mReverse) {
290+ camera. translate(0.0f , 0.0f , mDepthZ * interpolatedTime);
291+ } else {
292+ camera. translate(0.0f , 0.0f , mDepthZ * (1.0f - interpolatedTime));
293+ }
294+
295+ // 绕y轴旋转
296+ camera. rotateY(degrees);
297+
298+ camera. getMatrix(matrix);
299+ camera. restore();
300+
301+ // 调节中心点
302+ matrix. preTranslate(- centerX, - centerY);
303+ matrix. postTranslate(centerX, centerY);
304+ }
305+ }
306+ ```
307+
308+
309+
310+ 可以看到,短短的几十行代码就完成了,而核心代码(有注释部分)仅仅几行而已,简洁易懂。不过呢,这一份代码依旧是一份未完成的代码(不然怎么叫ApiDemo呢?),并且很多人不知道怎么修改。
311+
312+ 不知诸位在使用的时候可否发现了一个问题,同一份代码在不同手机上显示效果也是不同的,在像素密度较低的手机上,旋转效果比较正常,但是在像素密度较高的手机上显示效果则会很夸张,具体会怎样的,下面就来看一下具体效果。
313+
314+ ![ ] ( http://ww2.sinaimg.cn/large/005Xtdi2jw1f7sk60825wg308c0ea1kx.gif )
315+
316+ 可以看到,图片不仅因为形变失真,而且在中间一段因为形变过大导致图片无法显示,当然了,单个手机失真,你可以用` depthZ ` 忽悠过去,当 ` depthZ ` 设置的数值比较大大时候,图像在翻转同时会远离摄像头,距离远离,失真就不会显得很严重,不过这仍掩盖不了在不同手机上显示效果不同。
317+
318+ ** 如何解决这一问题呢?**
319+
320+ 想要解决其实也不难,只要修改两个数值就可以了,这两个数值就是在Matrix中一直被众多开发者忽略的 ` MPERSP_0 ` 和 ` MPERSP_1 `
321+
322+ ![ ] (http://latex.codecogs.com/png.latex ?
323+ $$
324+ \\left [
325+ \\begin{matrix}
326+ MSCALE\\_X & MSKEW\\_X & MTRANS\\_X \\\\
327+ \\\\
328+ MSKEW\\_Y & MSCALE\\_Y & MTRANS\\_Y \\\\
329+ \\\\
330+ MPERSP\\_0 & MPERSP\\_1 & MPERSP\\_2
331+ \\end{1}
332+ \\right ]
333+ $$)
334+
335+ 下面是修改后的代码(重点部分都已经标注出来了):
336+
337+ ```java
338+ public class Rotate3dAnimation extends Animation {
339+ private final float mFromDegrees;
340+ private final float mToDegrees;
341+ private final float mCenterX;
342+ private final float mCenterY;
343+ private final float mDepthZ;
344+ private final boolean mReverse;
345+ private Camera mCamera;
346+ float scale = 1; // <------- 像素密度
347+
348+ /**
349+ * 创建一个绕y轴旋转的3D动画效果,旋转过程中具有深度调节,可以指定旋转中心。
350+ * @param context <------- 添加上下文,为获取像素密度准备
351+ * @param fromDegrees 起始时角度
352+ * @param toDegrees 结束时角度
353+ * @param centerX 旋转中心x坐标
354+ * @param centerY 旋转中心y坐标
355+ * @param depthZ 最远到达的z轴坐标
356+ * @param reverse true 表示由从0到depthZ,false相反
357+ */
358+ public Rotate3dAnimation(Context context, float fromDegrees, float toDegrees,
359+ float centerX, float centerY, float depthZ, boolean reverse) {
360+ mFromDegrees = fromDegrees;
361+ mToDegrees = toDegrees;
362+ mCenterX = centerX;
363+ mCenterY = centerY;
364+ mDepthZ = depthZ;
365+ mReverse = reverse;
366+
367+ // 获取手机像素密度 (即dp与px的比例)
368+ scale = context.getResources().getDisplayMetrics().density;
369+ }
370+
371+ @Override
372+ public void initialize(int width, int height, int parentWidth, int parentHeight) {
373+ super.initialize(width, height, parentWidth, parentHeight);
374+ mCamera = new Camera();
375+ }
376+
377+ @Override
378+ protected void applyTransformation(float interpolatedTime, Transformation t) {
379+ final float fromDegrees = mFromDegrees;
380+ float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
381+ final float centerX = mCenterX;
382+ final float centerY = mCenterY;
383+ final Camera camera = mCamera;
384+ final Matrix matrix = t.getMatrix();
385+ camera.save();
386+
387+ // 调节深度
388+ if (mReverse) {
389+ camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
390+ } else {
391+ camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
392+ }
393+
394+ // 绕y轴旋转
395+ camera.rotateY(degrees);
396+
397+ camera.getMatrix(matrix);
398+ camera.restore();
399+
400+ // 修正失真,主要修改 MPERSP_0 和 MPERSP_1
401+ float[] mValues = new float[9];
402+ matrix.getValues(mValues); //获取数值
403+ mValues[6] = mValues[6]/scale; //数值修正
404+ mValues[7] = mValues[7]/scale; //数值修正
405+ matrix.setValues(mValues); //重新赋值
406+
407+ // 调节中心点
408+ matrix.preTranslate(-centerX, -centerY);
409+ matrix.postTranslate(centerX, centerY);
410+ }
411+ }
412+ ```
413+
414+ 修改后效果:
415+
416+ 
417+
418+
419+
420+ 上下对比差别还是很大的,顺便附上测试代码吧,layout文件就不写了,随便放一个ImageView就行了。
421+
422+ ```java
423+ setContentView(R.layout.activity_test_camera_rotate2);
424+ ImageView view = (ImageView) findViewById(R.id.img);
425+ assert view != null;
426+ view.setOnClickListener(new View.OnClickListener() {
427+ @Override
428+ public void onClick(View v) {
429+ // 计算中心点(这里是使用view的中心作为旋转的中心点)
430+ final float centerX = v.getWidth() / 2.0f;
431+ final float centerY = v.getHeight() / 2.0f;
432+
433+ //括号内参数分别为(上下文,开始角度,结束角度,x轴中心点,y轴中心点,深度,是否扭曲)
434+ final Rotate3dAnimation rotation = new Rotate3dAnimation(MainActivity.this, 0, 180, centerX, centerY, 0f, true, 2);
435+
436+ rotation.setDuration(3000); //设置动画时长
437+ rotation.setFillAfter(true); //保持旋转后效果
438+ rotation.setInterpolator(new LinearInterpolator()); //设置插值器
439+ v.startAnimation(rotation);
440+ }
441+ });
442+ ```
443+
444+
445+
446+ ## 相机位置
447+
448+ 我们可以使用translate和rotate来控制拍摄对象,也可以移动相机自身的位置,不过这些方法并不常用(看添加时间就知道啦)。
449+
450+ ```java
451+ void setLocation (float x, float y, float z); // (API 12) 设置相机位置,默认位置是(0, 0, -8)
452+
453+ float getLocationX (); // (API 16) 获取相机位置的x坐标,下同
454+ float getLocationY ();
455+ float getLocationZ ();
456+ ```
457+
458+ 我们知道近大远小,而物体之间的距离是相对的,让物体远离相机和让相机远离物体结果是一样的,实际上设置相机位置基本可以使用`translate`替代。
459+
460+ 虽然设置相机位置用处并不大,但还是要提几点注意事项:
461+
462+ #### 相机和View的z轴距离不能为0
463+
464+ 这个比较容易理解,当你把一个物体和相机放在同一个位置的时候,相机是拍摄不到这个物体的,正如你拿一张卡片放在手机侧面,摄像头是拍摄不到的。
465+
466+ #### 虚拟相机前后均可以拍摄
467+
468+ 当View不断接近摄像机并越过摄像机位置时,仍能看到View,并且View大小会随着距离摄像机的位置越来越远而逐渐变小,你可以理解为它有前置摄像头和后置摄像头。
469+
470+ #### 摄像机右移等于View左移
471+
472+ View的状态只取决于View和摄像机之间的相对位置,不过由于单位不同,摄像机平移一个单位等于View平移72个像素。下面两段代码是等价的:
473+
474+ ```java
475+ Camera camera = new Camera();
476+ camera.setLocation(1,0,-8); // 摄像机默认位置是(0, 0, -8)
477+ Matrix matrix = new Matrix();
478+ camera.getMatrix(matrix);
479+ Log.e(TAG, "location: "+matrix.toShortString() );
480+
481+ Camera camera2 = new Camera();
482+ camera2.translate(-72,0,0);
483+ Matrix matrix2 = new Matrix();
484+ camera2.getMatrix(matrix2);
485+ Log.e(TAG, "translate: "+matrix2.toShortString() );
486+ ```
487+
488+ 结果:
489+
490+ ```
491+ location: [1.0, 0.0, -72.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]
492+ translate: [1.0, 0.0, -72.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0
493+ ```
494+
495+ #### 要点
496+
497+ * View显示状态取决于View和摄像机之间的相对位置
498+ * View和相机的Z轴距离不能为0
499+
500+
501+
502+ **小技巧:关于摄像机和View的位置,你可以打开手机后置摄像头,拿一张卡片来回的转动平移或者移动手机位置,观察卡片在屏幕上的变化,**
503+
504+
505+
506+ ## 总结
507+
508+ 本篇主要讲解了关于Camera和Matrix的一些基础知识,Camera运用得当的话是能够制造出很多炫酷的效果的,我这里算是抛砖引玉,推荐一些比较炫酷的控件。
509+
510+ #### [从零开始打造一个Android 3D立体旋转容器](http://blog.csdn.net/mr_immortalz/article/details/51918560)
511+
512+ 
513+
514+
515+
516+ #### [FlipShare](https://github.com/JeasonWong/FlipShare)
517+
518+ 
226519
227520
228521
0 commit comments