DrawableCompat 和 Drawable 的区别

Difference between DrawableCompat and Drawable

几天前,支持库版本 v22.1.0 发布了,在新的东西中我看到了 DrawableCompat。

现在我的问题是 DrawableCompat 和普通的 Drawable 有什么区别,它带来什么好处以及何时以及如何使用它。

我已阅读 Chris Banes' blog,但未能正确解释这是什么以及它对开发人员有何用处。

引用有问题的博客post:

The Drawable tinting methods added in Lollipop are super useful for letting you dynamically tint assets.

此处,着色指的是 Theme.Material/Theme.AppCompat 使用灰度可绘制对象并应用色调的方法,例如 colorPrimarycolorAccent,将它们绑定到自定义中应用程序的主题。这个用在很多地方,特别是很多强调色的地方。

继续写博客 post:

AppCompat had its own baked in implementation in the v21 support library and we’ve now extracted that into DrawableCompat in support-v4 for everyone to use.

因此,DrawableCompat 允许您像 appcompat-v7 一样使用灰度图像着色,为可绘制对象着色,返回到 API 级别 4。这是对比从 API 级别 21 开始使用 Drawable 上可用的类似着色方法。特别是,如果您使用 appcompat-v7 创建自定义小部件 ,您可能会使用 DrawableCompat 让您的小部件采用应用的配色方案。

what is the difference between DrawableCompat and a normal Drawable

DrawableCompat 不同于典型的 ...Compat 方法。 类 和 NotificationCompat.Builder 一样是它们祖先的替代品(例如 Notification.Builder)。在这种情况下,DrawableCompat 将某些更改(例如色调)应用到另一个 Drawable,其中另一个可绘制对象是 "wrapped" by DrawableCompatDrawableCompat 仅提供 API 和 wrap/unwrap 功能;它是您从 wrap() 获得的 Drawable,您将在您的应用程序中使用它,而不是使用您开始时使用的原始灰度 Drawable