android - 在旋转动画集中,pivotX/Y 如何工作

标签 android android-layout rotation android-animation

我试图了解旋转动画的工作原理。
通过以下内容,我按如下方式旋转 View :

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="8000"
>

    <rotate android:fromDegrees="0"
            android:toDegrees="30"
            android:pivotX="100%"
            android:pivotY="100%"
            />

    <rotate
            android:toDegrees="100"
            android:pivotX="100%"
            android:pivotY="30%"
    />
</set>  

结果:
enter image description here

现在箭头所在的位置我尝试以它为中心再次旋转。
所以我修改了我的动画集如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="8000">
     <rotate android:fromDegrees="0"
                android:toDegrees="30"
                android:pivotX="100%"
                android:pivotY="100%"
                />  

        <rotate
                android:toDegrees="100"
                android:pivotX="100%"
                android:pivotY="30%"
        />  

     <rotate android:pivotY="-30%"
           android:pivotX="40%"
           android:toDegrees="100"/>    
</set>    

即我添加了

<rotate android:pivotY="-30%"
               android:pivotX="40%"
               android:toDegrees="100"/>  

这对我来说似乎是正确的,因为从屏幕上看,旋转点比 y 最左边的值小大约 30%,而 x 比 x 最左边的值大约多 40%。
但是当动画运行时,它并没有按预期工作。老实说,我不知道实际的旋转点是什么,整个 View 向左倾斜。
结果:

enter image description here

我在这里误解了什么?

最佳答案

枢轴是它旋转的点(就像在照片中放置图钉一样)。如果您的“从”和“到”枢轴不同,那么您不仅仅是围绕设定点旋转,而是旋转了一点,然后更改销钉位置并为每一步旋转更多一点(导致倾斜)。

以防万一您不知道:Android coordinates从左上角开始,而不是左下角。

我想你想要的只是那一套

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="8000">
     <rotate android:fromDegrees="0"
            android:toDegrees="130"
            android:pivotX="100%"
            android:pivotY="30%" />  
     <!-- I believe this, below will the the correct slide you need -->
     <translate android:fromXDelta="0" 
                android:toXDelta="-100%" />
</set>    

这将围绕图钉以 [100%, 30%] 旋转 130 度(x 是最大值,即屏幕的右边缘,y 是屏幕下方的 30%),同时时间向右滑动,直到它位于起始位置右侧的 100%( View 宽度)

关于android - 在旋转动画集中,pivotX/Y 如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42542043/

相关文章:

java - 如何使用谷歌的gson库

android - 如何在 zoom android 中应用长按和捏合?

css - 仅使用 CSS3 在悬停时旋转并在动画后停止?

android - textView 中缺少一些布局属性

javascript - THREE.js:需要帮助旋转四元数

Swift SpriteKit 在 x 轴旋转节点

android - 无论如何,是否可以使用cordova在Android和IOS中检测键盘语言?

android - 如何使用 webview 保存经过身份验证的网页以供离线使用?

c# - MonoDroid : Path to Assets to unzip a file with . 网络框架

java - Fragment 布局与 Activity Titlebar 重叠