Android 当视图沿Y轴旋转时,如何计算X轴缩小

Android 当视图沿Y轴旋转时,如何计算X轴缩小,android,android-animation,android-view,trigonometry,Android,Android Animation,Android View,Trigonometry,当视图沿Y轴旋转时,如何确定X轴缩减 我正在尝试创建一个结合旋转和平移的折叠动画 我有两个文本视图并排放置,如下xml所示: <LinearLayout android:id="@+id/colorBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_ma

当视图沿Y轴旋转时,如何确定X轴缩减

我正在尝试创建一个结合旋转和平移的折叠动画

我有两个文本视图并排放置,如下xml所示:

<LinearLayout
    android:id="@+id/colorBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/left"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@android:color/holo_blue_dark"
        android:text="A"
        android:textSize="100sp"/>

    <TextView
        android:id="@+id/right"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@android:color/holo_green_dark"
        android:text="B"
        android:textSize="100sp" />
</LinearLayout>
我这里的问题是如何计算
amountOfXToTranslate
的值

我尝试过各种数学计算

方法1:基于这样的推理:当一个视图旋转90度时,宽度为0,并且它完全消失;当一个视图根本没有旋转(旋转0度)时,宽度达到最大值。我假设当它旋转45度时,它沿X轴的宽度应该是其最大宽度的一半。在这种情况下,按照这个逻辑,假设我有两个文本视图,每个都是150dp宽,当它们都成45度角时,两个视图之间的空间量将是(150/2)*2。因此
amountOfXToTranslate
将为150

经过测试,我的假设被证明是错误的。出于某种原因,这一公式适用于某些尺寸,而对其他尺寸的则非常失败。(例如:当两个文本视图的宽度均为100dp,并且我进行了100dp转换时,动画看起来很好,并且两个视图之间没有间隙。但当两个视图的宽度均为150dp时,将存在间隙。因此,公式似乎适用于宽度小于150dp的情况)

方法2:因此,方法1的失败,我决定使用真实的数学方法,并在3维平面中考虑这一点。利用高中时教给我们的基础数学,三角函数: 我想我可以使用

AC=AB cos 45
的公式计算
amountOfXToTranslate
的值(AC和AB基于附加链接的图像)。因此,我计算要翻译的X的量的最后一个公式是
(widthOfTextView*cos45)*2
。不幸的是,在尝试了这个公式之后,它仍然是不正确的,并且X平移量不足,在两个视图之间留下了一个间隙

下面是我的问题和尝试的一些图片:

那么如何找到
amountOfXToTranslate
的值呢

这个问题部分与反向方程有关,但奇怪的是,反向方程不起作用(方法2)

编辑 嗯,我决定做一些手工调查工作,结果令人难以置信。 以下试验结果是在150dp的恒定视角高度下进行的,并沿Y轴旋转45度。
结果1:当每个视图的宽度为150px时,旋转后闭合两个视图之间间隙所需的平移为120px
结果2:当每个视图的宽度为300px时,旋转后闭合两个视图之间间隙所需的平移为290px
结果3:当每个视图的宽度为450px时,旋转后闭合两个视图之间间隙所需的平移为490px
结果4:当每个视图的宽度为600px时,旋转后闭合两个视图之间间隙所需的平移为712px

我只是觉得奇怪,当每个视图的宽度小于300时,所需的翻译量小于单个视图的宽度。但是,当每个视图的宽度大于300时,所需的平移量大于单个视图的宽度

left.setPivotX(0f);
left.setPivotY(left.getHeight()/2);
right.setPivotX(right.getWidth());
right.setPivotY(right.getHeight()/2);

ObjectAnimator rotateLeft;
rotateLeft = ObjectAnimator.ofFloat(left, "rotationY", left.getRotationY(), 45.0f);
ObjectAnimator rotateRight;
rotateRight = ObjectAnimator.ofFloat(right, "rotationY", right.getRotationY(), -45.0f);
ObjectAnimator translateRightX;
translateRightX = ObjectAnimator.ofFloat(right, "x", right.getX()-amountOfXToTranslate);

animateSet = new AnimatorSet();
animateSet.play(rotateRight).with(rotateLeft).with(translateRightX);
animateSet.setDuration(3000);
animateSet.setInterpolator(new DecelerateInterpolator());
animateSet.start();