上篇主要是通过人工方式给出所要变形的matrix,而这篇则借助于camera进行变换得到matrix直接应用到cellLayout中去,不多说了直接代码带解释
转载请注明http://ishelf.iteye.com/admin/blogs/836955
@Override public void dispatchDraw(Canvas canvas) { long start_time = System.currentTimeMillis(); startRotate(canvas, currentX, canvas.getWidth(), canvas.getHeight()); super.dispatchDraw(canvas); canvas.restore(); long end_time = System.currentTimeMillis(); Log.d("CellLayout" + currentScrenn, (end_time - start_time) + " ms"); } public void startRotate(Canvas mCanvas, float xCor, int width, int height) { boolean flag = true; if (isCurrentScrenn && xCor < 0) { xCor = width + xCor; flag = false; } else if (isCurrentScrenn && xCor >= 0) { // xCor = width - xCor; } else if (!isCurrentScrenn && xCor < 0) { xCor = width + xCor; } else if (!isCurrentScrenn && xCor >= 0) { flag = false; } if (xCor <= 0) { xCor = 10; }// the maximum left if (xCor > width) { xCor = width - 10; }// the maximum right float angle = 0; if (isBorder) { doDraw(mCanvas, width, height, angle, xCor); } else if (!flag) { angle = 90.0f - (xCor / (float) width) * 90.0f; doDraw(mCanvas, width, height, angle, xCor); } else { angle = -(xCor / (float) width) * 90.00f; doDraw(mCanvas, width, height, angle, xCor); } } private void doDraw(Canvas canvas, float width, float height, float angle, float cor) { canvas.save(); mCamera.save(); mCamera.rotateY(angle); mCamera.getMatrix(mMatrix); mCamera.restore(); // Log.w("CellLayout" + currentScrenn, angle + " __ " + mMatrix); if (angle < 0) { mMatrix.preTranslate(-width, -height * 0.5f); mMatrix.postTranslate(width, height * 0.5f); } else { mMatrix.preTranslate(0f, -height * 0.5f); mMatrix.postTranslate(0f, height * 0.5f); } canvas.concat(mMatrix); switch (currentScrenn) { case 0: mPaint.setColor(Color.RED); break; case 1: mPaint.setColor(Color.BLUE); break; case 2: mPaint.setColor(Color.YELLOW); break; case 3: mPaint.setColor(Color.CYAN); break; case 4: mPaint.setColor(Color.GREEN); break; } mPaint.setStyle(Paint.Style.FILL_AND_STROKE); canvas.drawRect(0, 0, width, height, mPaint); }
workspace的变化如上篇一样,这里就是引进了一个camera通过它进行变换绕Y轴的旋转变换并得到其matrix就可以了。camera使用了opengl的变化操作,所要控制效果要比人为控制matrix好些,但总体显示效果差不多。
从效率上将,如果机器支持3D加速,其速度要比2D快,不过差别也不是很大,本文做了以下一个比较
log-0-2D.txt 18.291666 log-0-3D.txt 7.9088144 log-1-2D.txt 22.92562 log-1-3D.txt 22.818924 log-2-2D.txt 29.503529 log-2-3D.txt 27.178694
每一行的数值表示canvas画图的时间,单位为毫秒,测试数据的平均执行次数在1000次左右,文件名中的0表示第1屏,2表示第二屏。从上面的数据可以看出,基本上相差不大,人工基本上感觉不出来。之所以第一屏差距有些大可能是因为第一屏有左边界。补充下测试的机器是真机, 大家以后来去数码市场悄悄 O(∩_∩)O哈哈~
以上只是demo,供以学习和讨论。欢迎大家给以指正