请允许我向您介绍Camera
类。不,不是硬件相机。
Camera | Android Developers
这是graphics
Camera
。鉴于标准缩放,旋转和平移动画仅在x-y平面中工作,Camera
引入了一个z轴,它在图像上给出透视图,并允许您在三维而不是两个坐标上进行转换。
您会对rotateY()
方法最感兴趣。这是可以帮助您创建正在翻转的卡片或磁贴的动画的方法。
以下是我使用的Camera
类瓷砖翻转:
private Matrix mMatrix;
private Camera mCamera;
// val is animation value between -1.0 and 1.0
private void updateImageMatrix(float val) {
// turn the animation value into an angle from
// 0 to -90, then +90 to 0 degrees
float degrees = 90.0F * (Math.signum(val) - val);
mCamera.save(); // save initial state
mCamera.rotateY(degrees); // this does the flip
mCamera.getMatrix(mMatrix); // write the transform into the matrix
mCamera.restore(); // now we can reuse the Camera without re-constructing
float centerX = imageWidth/2.0F;
float centerY = imageHeight/2.0F;
// Y-axis is at the left edge, so to get rotation
// around the center, we have to move the image over
// first, then move it back after we rotate
mRotateMatrix.preTranslate(- centerX, - centerY);
mRotateMatrix.postTranslate(centerX, centerY);
// this could probably be eliminated by initializing mCamera with
// mCamera.setLocation(centerX, centerY, mCamera.getLocationZ())
// the image probably isn't exactly the same size as the
// view, so we have to handle scaling the image to the view
mRotateMatrix.preScale(mScaleFactor, mScaleFactor);
}
现在你有一个矩阵做变换,你可以通过多种方式使用:
你可以有一个ImageView
这个矩阵集作为图像矩阵
你可以有一个自定义的View
塔牛逼覆盖onDraw
和呼叫canvas.drawBitmap(mBitmap, mMatrix, null)
你可以有一个自定义Animation
类中的覆盖applyTransformation(float interpolatedTime, Transformation t)
并返回一个基于矩阵更新Transformation
此外,通过翻转,你会想要动画第一图像从0度到-90度,然后隐藏该图像,并显示第二个图像,同时将剩余路径从90度动画到0度。
最后一件事。你有一个3x3网格。为了获得逼真的三维翻转效果,您需要使用不同的x和y值来实验mCamera.setLocation()
,具体取决于拼贴,以便顶部拼贴看起来像是从底部查看翻盖,底部拼贴看起来像你正在查看从头顶上翻转等。