栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ResourcesCompat.getDrawable()与AppCompatResources.getDrawable()

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ResourcesCompat.getDrawable()与AppCompatResources.getDrawable()

查看这两种方法的源代码,它们看起来非常相似。如果没有向量,则可能会使用其中之一。

ResourcesCompat.getDrawable()
将调用
Resources#getDrawable(int,theme)
API 21或更高版本。它还支持Android API 4+。仅此而已:

public Drawable getDrawable(Resources res, int id, Theme theme)        throws NotFoundException {    final int version = Build.VERSION.SDK_INT;    if (version >= 21) {        return ResourcesCompatApi21.getDrawable(res, id, theme);    } else {        return res.getDrawable(id);    }}

哪里

ResourcesCompatApi21
只打电话
res.getDrawable(id, theme)
。这意味着它将 不会
允许,如果设备不支持矢量绘图资源要绘制矢量绘图资源。但是,它将允许您传递主题。

同时,代码更改

AppCompatResources.getDrawable(Context context, intresId)
最终可以实现以下目标:

Drawable getDrawable(@NonNull Context context, @DrawableRes int resId, boolean failIfNotKnown) {    checkVectorDrawableSetup(context);    Drawable drawable = loadDrawableFromDelegates(context, resId);    if (drawable == null) {        drawable = createDrawableIfNeeded(context, resId);    }    if (drawable == null) {        drawable = ContextCompat.getDrawable(context, resId);    }    if (drawable != null) {        // Tint it if needed        drawable = tintDrawable(context, resId, failIfNotKnown, drawable);    }    if (drawable != null) {        // See if we need to 'fix' the drawable        DrawableUtils.fixDrawable(drawable);    }    return drawable;}

因此,此实例将尝试绘制资源(如果可以),否则它将在

ContextCompat
版本中查找以获取资源。然后,如有必要,它甚至会对其进行着色。但是,此方法仅支持API
7+。

所以我想决定是否应该使用其中之一,

  1. 您必须支持API 4、5或6吗?

    • 是:别无选择,只能使用
      ResourcesCompat
      ContextCompat
    • 否:继续前进到第二名。
    • 您是否绝对需要提供自定义主题?

    • 是:别无选择,只能使用

      ResourcesCompat

    • 否:使用
      AppCompatResources


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号