Android中的3D立方体过渡

Sir*_*get 15 android

我想让屏幕(或屏幕的一部分)切换到3D立方体过渡中的另一部分.我说的是2个普通的Android UI部件,而不是画布上渲染的原始图形.

你会怎么做?

谢谢

UPDATE

该项目很久以前就取消了,所以我没有实现这个目标.如果下面的答案有效,请对答案进行评论,我会将其标记为正确.

wea*_*ire 20

我想你要像一个交易

好吧,假设你有一个有两个孩子的观察者.您需要在绘制时单独访问每个孩子.您可以通过将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;
        }
Run Code Online (Sandbox Code Playgroud)

**

现在是位置部分.

**您需要知道的是每个孩子的位置.您可以通过获取每个子项相对于其父项的位置来实现.child.getLeft()为你这样做.和float centerChild = child.getLeft()+(child.getWidth()/2f).无论你做什么动画都必须将centerChild作为参考.

因此,您可以根据中心(centerChild)与父距离(getWidth()/2f)的距离,将子项旋转到x轴.

所以

float distanceFromCenter = getWidth() - thecenterChild;

现在你有了参考点.

**

现在到转换部分.

**

你必须改变转型.要做到这一点,你必须访问它的矩阵.

//Setup the matrix and alter it
 Matrix matrix = t.getMatrix();
 t.clear();
 t.setTransformationType(Transformation.TYPE_MATRIX);
Run Code Online (Sandbox Code Playgroud)

//在这里你可以玩矩阵..所以这样做

matrix.postTranslate(-distance, 0);
Run Code Online (Sandbox Code Playgroud)

你已经看到了一个有趣的动画.

然后让我们根据它的位置旋转图像.

matrix.postRotate(distance /40f);
matrix.preTranslate(-child.getWidth()/2f , -child.getHeight()/2f);
matrix.postTranslate(child.getWidth()/2f , child.getHeight()/2f);
Run Code Online (Sandbox Code Playgroud)

当viewswitcher动画时,你会得到一个有趣的旋转.

**

现在到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();
Run Code Online (Sandbox Code Playgroud)

所以现在让你的3D cude动画根据父距离计算旋转值并将其应用于每个孩子

mCamera.rotateX(度).

  • 请给我任何演示专家 (3认同)

Ren*_*eno 1

活动间转换之间的动画可以通过使用 来完成overridePendingTransition()

这是一个非常好的入门教程。


Jelly beans 支持LayoutTransistion和 ,ViewPropertyAnimator但我认为即使配备了它,您也无法实现 3D 立方体转换。

  • 本教程本身与 3D 立方体动画无关。无论如何+1:) (2认同)