【问题标题】:Programmatically start Animatable (AnimatedVectorDrawable) at a particular offset以编程方式在特定偏移处启动 Animatable (AnimatedVectorDrawable)
【发布时间】:2020-07-10 10:11:22
【问题描述】:

有没有办法以编程方式在特定偏移处启动AnimatedVectorDrawable

上下文:

我正在为 API 19 及更高版本进行开发,因此我使用的是 Android Jetpack。我有一个自定义的 View 带有动画“指示器”(带有矢量图像的 <selector>)。

我目前开始动画的方式如下:

ImageView indicator = findViewById(R.id.indicator);
indicatorDrawable = indicator.getDrawable().getCurrent();
if(indicatorDrawable instanceof Animatable) {
    ((Animatable) indicatorDrawable).start();
}

这是动画师,与我的“指标”相关联,在 xml 中:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="500"
        android:propertyName="fillAlpha"
        android:valueFrom="0.5"
        android:valueTo="0"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>

这一切都很好。但是,我在 Activity 中有多个自定义 View 实例,我需要同步它们的“指示器”动画(可以随时单独打开或关闭)。

我不会用细节来打扰你,但我目前有一个可接受的解决方案,它基本上在我的自定义View 中使用static 毫秒字段。它有效,但如果动画试图赶上,它会导致动画停止一段时间。

我宁愿立即在特定偏移处开始动画,以避免这种停滞。

我想我应该能够以某种方式访问​​与我的Animatable 关联的ObjectAnimator 上方,并设置它的startOffset,但我不知道如何。


那么,我能否以某种方式访问​​ ObjectAnimator 并以编程方式为每个 Animatable 单独设置其 startOffset

【问题讨论】:

    标签: java android android-animation android-vectordrawable objectanimator


    【解决方案1】:

    在互联网上进行了一些额外搜索后,我找到了库 Kyrie,作者: Alex Lockwood,对VectorDrawableAnimatedVectorDrawable的标准实现提出如下建议:

    但是,这两个类有三个主要限制:

    1. 不能暂停、恢复或查找它们。

    ...并解决这些问题。

    然后,经过一番搜索,我发现了一个新的 Android Jetpack 包,androidx.vectordrawable,它也解决了这些问题,特别是:androidx.vectordrawable:vectordrawable-seekable:1.0.0.

    因此,使用AnimatedVectorDrawable 的默认实现似乎根本不可能找到特定的偏移量。

    所以,现在我只需要弄清楚这个新的 Android Jetpack 包是如何工作的,看看它是否值得额外的开销。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2012-09-22
      • 1970-01-01
      相关资源
      最近更新 更多