Launcher 桌面的3D转屏效果实现(2)- Camera rotate
上篇主要是通过人工方式给出所要变形的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,供以学习和讨论。欢迎大家给以指正
不错,谢谢分享!
void moveToDefaultScreen(boolean animate) { Log.d(TAG, "moveToDefaultScreen"); if (animate) { snapToScreen(mDefaultScreen); } else { setCurrentScreen(mDefaultScreen); } getChildAt(mDefaultScreen).requestFocus(); }
/* if(f > 300) { // xCor = 310; xCor = width; value = 0.127225f; }//the maximum right */
/* private void doDraw(Canvas canvas,float width,float height,float angle,float xCor){ canvas.save(); mCamera.save(); mCamera.rotateY(angle); mCamera.getMatrix(mMatrix); mCamera.restore();
}
我的桌面3D效果,只改了workspace.java一个文件
lz,这是我改的workspace.java,我觉得改得不通用,在8寸机子(1280*768),3D效果不好,你帮我看看该怎么调。。。
public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller { @SuppressWarnings({"UnusedDeclaration"}) private static final String TAG = "Launcher.Workspace"; private static final int INVALID_SCREEN = -1;
//add by yzh for HTCpreview private static final int SENSE_OPENING = 1; private static final int SENSE_CLOSING = 2; private static final int SENSE_OPEN = 3; private static final int SENSE_CLOSED = 4; // sense zoom variables private boolean mSensemode = false; private boolean isAnimating = false; private long startTime; private int mStatus = SENSE_CLOSED; private final int mAnimationDuration = 400; private final int distro = { {1}, {2}, {1, 2}, {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 3, 2}, {3, 2, 3}, {3, 3, 3} }; private float previewScale = 1;
//end by yzh for HTCpreview
//------------------------add by yzh for 3D public static Bitmap s = new Bitmap; //这两个位图,转成3D效果 private int b1 = 0; private int b2 = 0;
//------------------------end by yzh for 3D
mCanvas = canvas;//add by yzh for 3D
// ((CellLayout) getChildAt(mCurrentScreen)).setDockBg(mLauncher.getDockbarCache());
//add by yzh for 3D if(mTouchState == TOUCH_STATE_SCROLLING){ startRotate(mCanvas,currentX,mCanvas.getWidth(),mCanvas.getHeight()); } //end by yzh for 3D }
//add by yzh for HTCpreview { private void findClickedPreview(float x, float y) { for (int i = 0; i < getChildCount(); i++) { RectF tmp = getScaledChild(getChildAt(i)); if (tmp.contains(x + getScrollX(), y + getScrollY())) { if (mCurrentScreen != i) { mLauncher.dismissPreviews(); snapToScreen(i); postInvalidate(); } else { mLauncher.dismissPreviews(); postInvalidate(); } } } } //end by yzh for HTCpreview
//add by yzh for HTCpreview public void openSense(boolean open) { //mScroller.abortAnimation(); int maxItemsPerRow = 0; int distro_set = getChildCount() - 1; int numRows = distro.length; for (int rows = 0; rows < distro.length; rows++) { if (distro > maxItemsPerRow) { maxItemsPerRow = distro; } } int maxPreviewHeight = (getMeasuredHeight() / numRows); float w = getMeasuredWidth() / maxItemsPerRow; int maxPreviewWidth = (int) w; float scaleW = ((float) maxPreviewWidth / (float) getWidth()); float scaleH = ((float) maxPreviewHeight / (float) getHeight()); previewScale = (scaleW > scaleH) ? scaleH : scaleW; if (previewScale >= 1) previewScale = .8f;
// @Override protected boolean drawChildForHTC(Canvas canvas, View child, long drawingTime) { int saveCount = canvas.save(); if (mSensemode) { if (isAnimating || mStatus == SENSE_OPEN) { long currentTime = SystemClock.uptimeMillis() - startTime; Rect r1 = new Rect(0, 0, child.getWidth(), child.getHeight()); RectF r2 = getScaledChild(child); float x = 0; float y = 0; float width = 0; float height = 0; float alpha = 255; if (mStatus == SENSE_OPENING) { alpha = easeOut(currentTime, 0, 100, mAnimationDuration); x = easeOut(currentTime, child.getLeft(), r2.left, mAnimationDuration); y = easeOut(currentTime, child.getTop(), r2.top, mAnimationDuration); width = easeOut(currentTime, child.getRight(), r2.right, mAnimationDuration); height = easeOut(currentTime, child.getBottom(), r2.bottom, mAnimationDuration); } else if (mStatus == SENSE_CLOSING) { alpha = easeOut(currentTime, 100, 0, mAnimationDuration); x = easeOut(currentTime, r2.left, child.getLeft(), mAnimationDuration); y = easeOut(currentTime, r2.top, child.getTop(), mAnimationDuration); width = easeOut(currentTime, r2.right, child.getRight(), mAnimationDuration); height = easeOut(currentTime, r2.bottom, child.getBottom(), mAnimationDuration); } else if (mStatus == SENSE_OPEN) { x = r2.left; y = r2.top; width = r2.right; height = r2.bottom; alpha = 100; } float scale = ((width - x) / r1.width()); canvas.save(); canvas.translate(x, y); canvas.scale(scale, scale); mPaint.setAlpha((int) alpha); canvas.drawRoundRect(new RectF(r1.left + 5, r1.top + 5, r1.right - 5, r1.bottom - 5), 15f, 15f, mPaint); mPaint.setAlpha(255); child.draw(canvas); canvas.restore(); } else { child.draw(canvas); } } else { super.drawChild(canvas, child, drawingTime); } canvas.restoreToCount(saveCount); return true; }
//end by yzh for HTCpreview
CellLayout
根据需要吧,其实都是正对canvas的一个变换
lz,这桌面3D效果能只在Workspace.java里改吗?不要在CellLayout.java里改
这个翻转是针对workspace的,下方的三个图标是在另外一个布局中,需要单独做...
lz,能实现屏和屏的下方3个图标一起翻吗?就是其中一个屏和下方3个图标一起翻,或者是所有屏和下方3个图标一起翻。
源码包是没有了,呵呵, 基本重点的都写出来了, 交流用了.....
lz 可否发份完整代码压缩包给我研究研究 wensefu@foxmail.com 谢谢。你发的代码中一些变量的声明以及值的变化的地方 还有sepa方法木有。
不错,谢谢分享!