简体   繁体   English

AnimatedVectorDrawable 支持 < 24

[英]AnimatedVectorDrawable support < 24

I have an animated vector drawable asset in my drawables folder.我的 drawables 文件夹中有一个动画矢量可绘制资产。 I use the following code to run it on button click我使用以下代码在按钮单击时运行它

val myVectorDrawable = ResourcesCompat.getDrawable(
            resources,
            R.drawable.animation,
            theme
        )


        button.setOnClickListener {
            image.setImageDrawable(null)
            image.setImageDrawable(myVectorDrawable)

            val drawable = image.drawable

            if (drawable is AnimatedVectorDrawableCompat) {
                drawable.start()
            } else if (drawable is AnimatedVectorDrawable)
                drawable.start()

        }

This runs perfectly if the device runs an android version > 24 and crashes otherwise.如果设备运行 android 版本 > 24,则此运行完美,否则会崩溃。 I need to support android devices with minimum SDK 21.我需要支持最低 SDK 21 的 Android 设备。

My questions are我的问题是

  1. How to make my code support devices with 21 up to 24 .如何使我的代码支持2124
  2. is there a better way to run AnimatedVectorDrawable animation有没有更好的方法来运行AnimatedVectorDrawable动画

If you know you are using an animated vector, you can use AnimatedVectorDrawableCompat.create() to create an AnimatedVectorDrawableCompat instance that is available on all API 14+ devices:如果您知道您使用的是动画矢量,则可以使用AnimatedVectorDrawableCompat.create()创建一个可在所有 API 14+ 设备上使用的AnimatedVectorDrawableCompat实例:

val drawable = AnimatedVectorDrawableCompat.create(
    this, // your Context
    R.drawable.animation)

button.setOnClickListener {
    image.setImageDrawable(null)
    image.setImageDrawable(drawable)

    drawable.start()
}

However, if you want a more generic approach, you must instead use AppCompatResources.getDrawable() instead of ResourcesCompat.getDrawable() as that properly takes into account the VectorDrawableCompat , AnimatedVectorDrawableCompat , and AnimatedStateListDrawableCompat classes in a way that is compatible with all API levels:但是,如果您想要更通用的方法,则必须改用AppCompatResources.getDrawable()而不是ResourcesCompat.getDrawable()因为它以与所有 API 级别兼容的方式正确考虑了VectorDrawableCompatAnimatedVectorDrawableCompatAnimatedStateListDrawableCompat类:

val drawable = AppCompatResources.getDrawable(
    this, // your Context
    R.drawable.animation)

button.setOnClickListener {
    image.setImageDrawable(null)
    image.setImageDrawable(drawable)

    if (drawable is Animatable) {
        drawable.start()
    }
}

Did you configure your build to use the support library implementation?您是否将构建配置为使用支持库实现?

https://developer.android.com/guide/topics/graphics/vector-drawable-resources#vector-drawables-backward-solution https://developer.android.com/guide/topics/graphics/vector-drawable-resources#vector-drawables-backward-solution

 android { defaultConfig { vectorDrawables.useSupportLibrary = true } }

Without this, the build system will create fallback (non-vector) resources for lower SDK versions instead of using the support implementation.如果没有这个,构建系统将为较低的 SDK 版本创建回退(非矢量)资源,而不是使用支持实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM