【问题标题】:Using ObjectAnimator to do animations in AndroidAndroid中使用ObjectAnimator做动画
【发布时间】:2018-02-26 09:46:44
【问题描述】:

如何使用 ObjectAnimator 为视图的 X 属性设置动画,但只需在 xml 动画文件中设置起始位置,如下所示:

<objectAnimator 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueType="floatType"
    android:propertyName="x"
    android:valueFrom="100.0" />

以及布局中的最终位置(因为在我的情况下,视图的最终位置将相对于布局中的其他视图)。 因此,我无法通过以下方式获得更精确的观点:

<objectAnimator 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueType="floatType"
    android:propertyName="x"
    android:valueFrom="100.0"
    android:valueFrom="300.0" />

这很容易通过使用视图动画而不是属性动画并使用 fromXDelta 属性来实现。

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromXDelta="100%p"/>
</set>

这将使视图从屏幕宽度的 100% 移动到布局中设置的位置。

【问题讨论】:

  • 我不明白你的意思,你到底想达到什么目的?
  • @pskink 我已将问题编辑得更清楚。究竟什么是你没有得到的问题?
  • 只需使用您自己的属性 - 例如将其称为“hiddenDroid”,然后只需创建一个方法 setHiddenDroid(float f) { ... 并在此处使用您的视图做任何您想做的事情
  • 我不认为你明白我的意思。因为 setProperty() 不适用于这种情况
  • 运行thissetProperty 是什么意思?

标签: android xml android-animation objectanimator


【解决方案1】:

这可能会帮助您在获得布局的最终​​位置时以编程方式执行动画:

ObjectAnimator anim = ObjectAnimator.ofFloat(solution_board_layout, 
  "translationX", 0, 300);
  anim.setDuration(0);
  anim.start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2016-11-04
    相关资源
    最近更新 更多