Android中的3D立方体过渡

时间:2011-03-17 13:48:08

标签: android

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

你会怎么做?

由于

更新

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

2 个答案:

答案 0 :(得分:20)

我想你想要一个像this

这样的交易

好吧,假设你有一个有两个孩子的观察家。 您需要在绘制时单独访问每个孩子。您可以通过将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() - thecenterChild;

现在你有了参考点。

**

  

现在转换部分。

**

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

//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);

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

**

  

现在转到3D Part!

**

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(度)。

答案 1 :(得分:1)

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

这是一个非常好的tutorial开始。


Jelly beans支持LayoutTransistionViewPropertyAnimator,但我不认为即便配备此功能,您也可以完成3D立方体转换。