Android 百分比库中的动画

Android Animations on Percent Library

我一直在研究 Android 上的动画主题,但在针对使用 Percent 库的项目实施这些发现时遇到了困难。

特别是,我的布局中有以下元素 xml:

 <ImageView
        android:id="@+id/aImage"
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_marginTopPercent="35%"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"/>

将以下属性分配给根 PercentRelativeLayout 元素:

xmlns:app="http://schemas.android.com/apk/res-auto"

现在,我想创建一个 AnimatorSet 对象,它可以执行多个操作,重点放在 app:layout_marginTopPercent="35%" 属性上。

我已经尝试创建一个 ObjectAnimator 并将其添加到 AnimatorSet,但这没有任何效果:

ObjectAnimator anim1 = ObjectAnimator.ofFloat(logoImageView, "layout_marginTopPercent", 0.35f, 0.1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(anim1);
animatorSet.start(); 

谁能解释一下我哪里出错了。有趣的是,我能够创建动画 .xml 文件并使用 translate 元素执行成功的动画,但我需要在代码中执行此操作。下面是一个成功的例子。XML动画:

<set>
        <translate
         android:fromYDelta="0%p"
         android:toYDelta="-25%p"
         android:duration="1000" />
</set>

提前致谢

我的库 ViewPropertyObjectAnimator 的新版本 (>=1.3.0) 提供了一种从 Percent Support Library 动画化 percent 参数的方法。获得 ObjectAnimator(您可以在 AnimatorSet 中使用)与使用 ViewPropertyAnimator.

一样简单
ObjectAnimator logoMarginAnimator = 
            ViewPropertyObjectAnimator.animate(logoImageView).topMarginPercent(0.1f).get();
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(logoMarginAnimator);
animatorSet.start();