以编程方式在特定偏移处启动 Animatable (AnimatedVectorDrawable)
Programmatically start Animatable (AnimatedVectorDrawable) at a particular offset
有没有办法以编程方式在特定偏移处启动 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
?
在互联网上进行一些额外搜索后,我找到了图书馆 Kyrie,通过
Alex Lockwood,这表明以下关于 VectorDrawable
和 AnimatedVectorDrawable
的标准实现:
However, these two classes have three main limitations:
- They can't be paused, resumed, or seeked.
...并解决这些问题。
然后,经过更多搜索,我发现了一个新的 Android Jetpack 包,androidx.vectordrawable,它也解决了这些问题,特别是:androidx.vectordrawable:vectordrawable-seekable:1.0.0.
因此,似乎无法使用 AnimatedVectorDrawable
的默认实现来寻找特定偏移量。
所以,现在我只需要弄清楚这个新的 Android Jetpack 包是如何工作的,看看它是否值得额外的开销。
有没有办法以编程方式在特定偏移处启动 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
?
在互联网上进行一些额外搜索后,我找到了图书馆 Kyrie,通过
Alex Lockwood,这表明以下关于 VectorDrawable
和 AnimatedVectorDrawable
的标准实现:
However, these two classes have three main limitations:
- They can't be paused, resumed, or seeked.
...并解决这些问题。
然后,经过更多搜索,我发现了一个新的 Android Jetpack 包,androidx.vectordrawable,它也解决了这些问题,特别是:androidx.vectordrawable:vectordrawable-seekable:1.0.0.
因此,似乎无法使用 AnimatedVectorDrawable
的默认实现来寻找特定偏移量。
所以,现在我只需要弄清楚这个新的 Android Jetpack 包是如何工作的,看看它是否值得额外的开销。