这是我的 build.gradle 文件
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
vectorDrawables.useSupportLibrary = true
}
以及布局的一部分
<ImageView
android:id="@+id/recents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/anim_test"/>
和类转换:
val np = convertView.findViewById<ImageView>(R.id.recents)
val anim = np.drawable as AnimatedVectorDrawableCompat
这在Lolipop(sdk 21)上按预期工作,但在Nougat上失败,显示以下消息:
android.graphics.drawable.AnimatedVectorDrawable cannot be cast to android.support.graphics.drawable.AnimatedVectorDrawableCompat
我不理解的是,为什么在SDK 21级别上返回AnimatedVectorDrawableCompat,当系统已经支持AnimatedVectorDrawable。而且,尽管指定了,在Nougat中它也会返回AnimatedVectorDrawable。