在Android的3D魔方转型 [英] 3D cube transition in Android

查看:228
本文介绍了在Android的3D魔方转型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个屏幕(或屏幕的一部分)切换到在3D立方体状的过度另一部分。 我说的是2个正常的Andr​​oid UI部分,在画布上呈现的不是原始的图形。

I want to make a screen (or part of the screen) switch to another part in a 3D cube-like transition. I'm talking about 2 normal Android UI parts, not raw graphics rendered on a canvas.

你会如何处理这个?

感谢

更新

该项目被取消了很久以前,所以我没能实现这一点。若跌破作品的回答,请评价等方面的答案,我会标记为正确的。

The project was cancelled a long time ago so I didn't get to implement this. If the answer below works, please comment so on the answer and I'll mark it as correct.

推荐答案

我想你要像这样

好吧,让我们说,你有2个孩子一个viewswitcher。 您需要访问每个孩子单独他们画的时候。你可以做到这一点的ViewSwicher类延伸到自己的MyViewSwitcher,使静态转换

Ok let's say you have a viewswitcher with 2 children. You need to access each child separately when they draw. You can do that by extending the ViewSwicher class to your own MyViewSwitcher and enable static transformations

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()这是否适合你。 和浮动centerChild = child.getLeft()+(child.getWidth()/ 2F)。无论动画,你做 已经有centerChild作为参考。

** What you need to know is the position of each child. You can do that by getting the position of each child relative to its parent. child.getLeft() does that for you. and float centerChild = child.getLeft()+(child.getWidth()/2f) . Whatever animation you do has to have centerChild as a reference.

所以,你可以告诉旋转孩子根据它的中心的距离x轴(<$ C C $> centerChild )已经从父母的距离(的getWidth( )/ 2F )。

So you can tell rotate the child to the x axis based on the distance it's center ( centerChild) has from the parents distance (getWidth()/2f) .

于是

浮动distanceFromCenter =的getWidth()-centerChild;

float distanceFromCenter = getWidth() -centerChild;

现在,你有你的参考点。

Now you have your reference points.

**

现在到了转变的部分。

**

您需要改变的转变。要做到这一点,你需要访问它的矩阵。

You have to alter the transformation. To do that you have to access it's matrix.

//Setup the matrix and alter it
 Matrix matrix = t.getMatrix();
 t.clear();
 t.setTransformationType(Transformation.TYPE_MATRIX);

//在这里,您可以与基体玩.. 这样做

//Here you can play with the matrix.. so by doing

matrix.postTranslate(-distance, 0);

您已经获得了滑稽的动画。

you already get a funny looking animation.

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

Then let's rotate the image based on it's position.

matrix.postRotate(distance /40f);
matrix.preTranslate(-child.getWidth()/2f , -child.getHeight()/2f);
matrix.postTranslate(child.getWidth()/2f , child.getHeight()/2f);

您得到一个好笑的看着旋转的时候viewswitcher动画。

You get a funny looking rotation when the viewswitcher animates.

**

现在的3D零件!

**

Android的可访问摄像机类。 android.graphics.Camera

Android gives access to the camera class. android.graphics.Camera

- 相机类所做的是它改变了3x3矩阵,并提供3D转换。

What camera class does is it modifies the 3x3 matrix and provides 3D transformations.

在getChildStaticTransformation()...

Inside 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基于父距离只图的旋转值,并将其与适用于每个孩子

So now to get your 3D cude animation just figure the rotation values based on the parent distance and apply it to each child with

mCamera.rotateX(度)。

mCamera.rotateX(deg).

这篇关于在Android的3D魔方转型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆