我想你想要一个像this这样的交易
好的,假设您有一个带有 2 个孩子的视图切换器。
当他们画画时,您需要分别访问每个孩子。您可以通过将 ViewSwicher 类扩展到您自己的 MyViewSwitcher 并启用静态转换来做到这一点
public class MyViewSwitcher extends ViewSwitcher {
//DO THAT FOR ALL CONSTRUCTORS
public PViewSwitcher(Context context) {
super(context);
setStaticTransformationsEnabled(true);
....
}
//Now you have to override getChildStaticTransformation
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
//enable drawing cache to each child to get smoother transactions
child.setDrawingCacheEnabled(true);
//To identify if the child is the first or the second of the viewSwitcher do that
if (child == getChildAt(0)) {
//Here i will transform the first child
}
if (child == getChildAt(1)) {
//Here i will transform the second child
}
return true;
}
**
现在是位置部分。
**
你需要知道的是每个孩子的位置。您可以通过获取
每个孩子相对于其父母的位置。 child.getLeft() 为您做到这一点。
和float centerChild = child.getLeft()+(child.getWidth()/2f)。无论你做什么动画
必须有 centerChild 作为参考。
因此,您可以根据孩子的中心 (centerChild) 与父母距离 (getWidth()/2f) 的距离来判断将孩子旋转到 x 轴。
所以
float distanceFromCenter = getWidth() -centerChild;
现在您有了参考点。
**
现在进入转换部分。
**
您必须更改转换。为此,您必须访问它的矩阵。
//Setup the matrix and alter it
Matrix matrix = t.getMatrix();
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
//这里可以玩矩阵..
所以通过这样做
matrix.postTranslate(-distance, 0);
你已经得到了一个有趣的动画。
然后让我们根据它的位置旋转图像。
matrix.postRotate(distance /40f);
matrix.preTranslate(-child.getWidth()/2f , -child.getHeight()/2f);
matrix.postTranslate(child.getWidth()/2f , child.getHeight()/2f);
当视图切换器动画时,你会看到一个有趣的旋转。
**
现在进入 3D 部分!
**
Android 允许访问相机类. android.graphics.Camera
相机类的作用是修改 3x3 矩阵并提供 3D 变换。
在 getChildStaticTransformation() 内部...
Camera mCamera;
mCamera.save();
mCamera.rotateY(distance/40f); //you should divide by the parent's width so your rotation values are 0>=rotation >= MAX_ROTATION
mCamera.getMatrix(matrix);
mCamera.restore();
所以现在要获得您的 3D cude 动画,只需根据父距离计算旋转值并将其应用于每个子节点
mCamera.rotateX(deg).