![API 16 animation](https://istack.dev59.com/ZCLhA.gif)
解析Drawable
在此支持库中,可以通过以下方式解析`VectorDrawable`
和`AnimatedVectorDrawable`
:
//这将只会解析一个根元素为<vector>的可绘制对象
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
//这将只会解析一个根元素为<animated-vector>的可绘制对象
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
// 这将解析任何可绘制对象,并且在api 21+设备上自动回退到lollipop实现
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);
如果在Java代码中解析Drawable,则建议始终使用ResourcesCompat.getDrawable()
,因为它处理了Lollipop回退(如果适用)。这允许系统缓存Drawable ConstantState,因此更有效率。
该库具有以下变形(双向)动画:
播放-暂停形变动画
播放-停止形变动画
箭头-汉堡菜单形变动画
如您所见,我在我的
API 16
手机上生成了上述图像:
import com.wnafee.vector.compat.AnimatedVectorDrawable
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector)
请查看 vector-compat 的 github README,链接在这里:https://github.com/wnafee/vector-compat
如果你将其与应用程序模块的 build.gradle
dependencies
(通常位于文件末尾)合并,它将解决您的问题(至 API 14
)。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.wnafee:vector-compat:1.0.5'
}