Android 动画 ObjectAnimator 从中心旋转

标签 android animation objectanimator

另一个我似乎无法理解的简单问题。我想要一个使用 ObjectAnimator 从中心向上缩放的动画。但是,我不确定如何设置 PivotX/Y 属性,因为我应用的任何值似乎都不会影响 View 。当我使用 scaleanimation 时,它工作正常,但我必须在这里使用 ObjectAnimator。

我试过了

ObjectAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX",0.0f,1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY",0.0f,1.0f);

//I've tried a wide range of values from 0,0 to 0,0.5, to 0.5, 0.5 but none of them do anything
ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",0,1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",0,1f);

//I've also tried view.setPivotX(0.5f) but that didn't do anything either

animatorSet.playTogether(scaleX,scaleY,pivotX,pivotY);
animatorSet.start();

我只是不太确定如何让它从中心开始缩放。我试过甚至不使用 pivot,但这也没有做任何事情。

感谢任何帮助,

谢谢

** 编辑 **

下面的作品,除了它没有完全居中,而是向左上角增长,但仍然有点居中。很难描述。我试过使用 0.5f 也没有用

ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",1f);

最佳答案

我在缩放以 XML 定义的矢量动画时遇到了类似的问题。现在,在将 pivotX 和 pivotY 设置放置在 Drawable 而不是 ObjectAnimator 之后,它会从中心开始缩放,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:aapt="http://schemas.android.com/aapt" >
    <aapt:attr name="android:drawable">
        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="100dp"
            android:height="100dp"
            android:viewportWidth="100"
            android:viewportHeight="100">
            <group
                android:name="scaleGroup"
                android:pivotX="50"
                android:pivotY="50"
                android:scaleX="1.0"
                android:scaleY="1.0" >
                <path
                    android:name="v"
                    android:strokeColor="#00e9bd"
                    android:strokeWidth="6"
                    android:pathData="M 50 13 C 70.4345357437 13 87 29.5654642563 87 50 C 87 70.4345357437 70.4345357437 87 50 87 C 29.5654642563 87 13 70.4345357437 13 50 C 13 29.5654642563 29.5654642563 13 50 13 Z" />
            </group>
        </vector>
    </aapt:attr>

    <target android:name="scaleGroup"> *
        <aapt:attr name="android:animation">
            <set>
                <objectAnimator
                   android:duration="1000"
                   android:propertyName="scaleX"
                   android:valueFrom="0.5"
                   android:valueTo="1.0" />
               <objectAnimator
                   android:duration="1000"
                   android:propertyName="scaleY"
                   android:valueFrom="0.5"
                   android:valueTo="1.0" />
            </set>
        </aapt:attr>
    </target>
</animated-vector>

关于Android 动画 ObjectAnimator 从中心旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003381/

相关文章:

android - 单击android通知后退出应用程序

html - 使用 CSS 为句子中的单个单词制作动画

javascript - Highcharts Arareange 超过一定长度没有缩放动画

java - 使用相同的动画为多个 View 设置动画

android - LayoutTransition 消失和改变不会同时发生

android - 如何在 Android 中恢复和暂停 API 级别低于 19 的 ObjectAnimator?

android - 当您上传未使用上传证书签名的 APK 时;你必须使用相同的证书吗?

android - 执行完成后关闭 Realm 实例

android - 如何在 TextView 中制作所有 3 种类型的链接

python - (Tkinter) 放置对象后如何运行动画?