【问题标题】:3D cube transition in Android [closed]Android中的3D立方体过渡[关闭]
【发布时间】:2011-03-17 13:48:07
【问题描述】:

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

你会如何处理这个问题?

谢谢

更新

这个项目很久以前就被取消了,所以我没能实施。如果以下答案有效,请对答案发表评论,我会将其标记为正确。

【问题讨论】:

  • 你实现过这个吗?我正在寻找相同的答案。
  • 因为你不能用蹩脚的标准动画子类组成一个类似立方体的动画——而且你不能在 XML 中指定自定义动画实现,这应该是相当“不可修复”的,请参阅 this bug 没有收到来自 Google 的爱。
  • 你有什么解决办法吗?
  • 这个项目很久以前就被取消了,所以我没能实现这个。如果以下答案有效,请对答案发表评论,我会标记它。
  • 致结束问题的人 - 这里有什么不清楚的地方? 3D 立方体状的过渡似乎是一个非常精确的描述。这个问题被投票了 13 次,所以我想大多数读者都理解它。查看您个人资料中的标签,我什至没有在其中看到“Android” - 为什么要在您不了解的知识领域乱搞?

标签: android


【解决方案1】:

我想你想要一个像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).

【讨论】:

  • 请分享更多信息如何实现?
  • @weakwire 分享如何实施的任何信息
  • 我能得到任何样本吗?我正在我的项目中实施
  • 由于这个答案得到了投票,但没有人报告它确实有效(而且我的项目很久以前就被取消了),如果答案有效,请发表评论。如果是,我会将其标记为答案。
  • 在这里查看演示http://stackoverflow.com/questions/13245031/how-to-implement-cube-transition-effect-animation-on-viewpager-in-android/27774196#27774196
【解决方案2】:

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

这是一个很好的tutorial 开始。


Jelly beans 支持 LayoutTransistionViewPropertyAnimator,但我认为即使有了这个,你也无法实现 3D 立方体转换。

【讨论】:

  • 教程本身与动画之类的3d立方体无关。无论如何+1 :)
  • 是的,我知道谢谢 :) 我希望 OP 必须自己制作动画。如果我确实找到了一种方法(不使用画布上的常用图形方法),我将编辑我的答案。
  • @Reno 这个问题怎么解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 2013-05-20
  • 2015-08-04
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
相关资源
最近更新 更多