Launcher 桌面的3D转屏效果实现(2)- Camera rotate

来源:互联网 发布:淘宝卖家手机旺旺 编辑:程序博客网 时间:2024/06/15 18:31

上篇主要是通过人工方式给出所要变形的matrix,而这篇则借助于camera进行变换得到matrix直接应用到cellLayout中去,不多说了直接代码带解释

    转载请注明http://ishelf.iteye.com/admin/blogs/836955

 

Java代码  收藏代码
  1. @Override  
  2.   
  3.   
  4. public void dispatchDraw(Canvas canvas) {  
  5.     long start_time = System.currentTimeMillis();  
  6.     startRotate(canvas, currentX, canvas.getWidth(), canvas.getHeight());  
  7.     super.dispatchDraw(canvas);  
  8.     canvas.restore();  
  9.     long end_time = System.currentTimeMillis();  
  10.     Log.d("CellLayout" + currentScrenn, (end_time - start_time) + " ms");  
  11. }  
  12.   
  13.   
  14.   
  15.   
  16.   
  17. public void startRotate(Canvas mCanvas, float xCor, int width, int height) {  
  18.     boolean flag = true;  
  19.     if (isCurrentScrenn && xCor < 0) {  
  20.         xCor = width + xCor;  
  21.         flag = false;  
  22.     } else if (isCurrentScrenn && xCor >= 0) {  
  23.         // xCor = width - xCor;  
  24.     } else if (!isCurrentScrenn && xCor < 0) {  
  25.         xCor = width + xCor;  
  26.     } else if (!isCurrentScrenn && xCor >= 0) {  
  27.         flag = false;  
  28.     }  
  29.     if (xCor <= 0) {  
  30.         xCor = 10;  
  31.     }// the maximum left  
  32.     if (xCor > width) {  
  33.         xCor = width - 10;  
  34.     }// the maximum right  
  35.     float angle = 0;  
  36.     if (isBorder) {  
  37.         doDraw(mCanvas, width, height, angle, xCor);  
  38.     } else if (!flag) {  
  39.         angle = 90.0f - (xCor / (float) width) * 90.0f;  
  40.         doDraw(mCanvas, width, height, angle, xCor);  
  41.     } else {  
  42.         angle = -(xCor / (float) width) * 90.00f;  
  43.         doDraw(mCanvas, width, height, angle, xCor);  
  44.     }  
  45. }  
  46.   
  47. private void doDraw(Canvas canvas, float width, float height, float angle, float cor) {  
  48.     canvas.save();  
  49.     mCamera.save();  
  50.     mCamera.rotateY(angle);  
  51.     mCamera.getMatrix(mMatrix);  
  52.     mCamera.restore();  
  53.     // Log.w("CellLayout" + currentScrenn, angle + " __  " + mMatrix);  
  54.     if (angle < 0) {  
  55.         mMatrix.preTranslate(-width, -height * 0.5f);  
  56.         mMatrix.postTranslate(width, height * 0.5f);  
  57.     } else {  
  58.         mMatrix.preTranslate(0f, -height * 0.5f);  
  59.         mMatrix.postTranslate(0f, height * 0.5f);  
  60.     }  
  61.    canvas.concat(mMatrix);  
  62.     switch (currentScrenn) {  
  63.         case 0:  
  64.             mPaint.setColor(Color.RED);  
  65.             break;  
  66.         case 1:  
  67.             mPaint.setColor(Color.BLUE);  
  68.             break;  
  69.         case 2:  
  70.             mPaint.setColor(Color.YELLOW);  
  71.             break;  
  72.         case 3:  
  73.             mPaint.setColor(Color.CYAN);  
  74.             break;  
  75.         case 4:  
  76.             mPaint.setColor(Color.GREEN);  
  77.             break;  
  78.     }  
  79.   
  80.    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);  
  81.     canvas.drawRect(00, width, height, mPaint);  
  82. }  

 

 

    workspace的变化如上篇一样,这里就是引进了一个camera通过它进行变换绕Y轴的旋转变换并得到其matrix就可以了。camera使用了opengl的变化操作,所要控制效果要比人为控制matrix好些,但总体显示效果差不多。

     从效率上将,如果机器支持3D加速,其速度要比2D快,不过差别也不是很大,本文做了以下一个比较

 

Html代码  收藏代码
  1. log-0-2D.txt  18.291666  
  2. log-0-3D.txt  7.9088144  
  3. log-1-2D.txt  22.92562  
  4. log-1-3D.txt  22.818924  
  5. log-2-2D.txt  29.503529  
  6. log-2-3D.txt  27.178694  

 

     每一行的数值表示canvas画图的时间,单位为毫秒,测试数据的平均执行次数在1000次左右,文件名中的0表示第1屏,2表示第二屏。从上面的数据可以看出,基本上相差不大,人工基本上感觉不出来。之所以第一屏差距有些大可能是因为第一屏有左边界。补充下测试的机器是真机, 大家以后来去数码市场悄悄 O(∩_∩)O哈哈~

      以上只是demo,供以学习和讨论。欢迎大家给以指正


原创粉丝点击