ResourcesCompat.getDrawable()vs AppCompatResources.getDrawable() [英] ResourcesCompat.getDrawable() vs AppCompatResources.getDrawable()

查看:1243
本文介绍了ResourcesCompat.getDrawable()vs AppCompatResources.getDrawable()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这两个API感到困惑。

I'm a bit confused with these two APIs.

ResourcesCompat.getDrawable(Resources res ,int id,Resources.Theme theme)

ResourcesCompat.getDrawable(Resources res, int id, Resources.Theme theme)


返回与特定资源ID关联的可绘制对象为指定的主题设计风格。根据底层资源,将返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等。

Return a drawable object associated with a particular resource ID and styled for the specified theme. Various types of objects will be returned depending on the underlying resource -- for example, a solid color, PNG image, scalable image, etc.

在API级别21之前,主题将不会应用,此方法只需调用getDrawable(int)。

Prior to API level 21, the theme will not be applied and this method simply calls through to getDrawable(int).

AppCompatResources.getDrawable(Context context, int resId)

AppCompatResources.getDrawable(Context context, int resId)


返回与特定资源ID关联的可绘制对象。

Return a drawable object associated with a particular resource ID.

此方法支持在没有平台支持的设备上充斥矢量动画矢量资源。

This method supports inflation of vector and animated-vector resources on devices where platform support is not available.



问题



Question


  1. 这两个类之间的显着差异
    (除了 vector 通货膨胀)?

  2. 我应该选择哪一个?为什么?

  1. What is the significant difference between these two classes (besides vector inflation)?
  2. Which one should I prefer to another and why?


推荐答案

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

Looking at the source code of the two methods, they seem very similar. If you don't have vectors, you could probably get away with using either one or the other.

ResourceCompat.getDrawable()将在API 21或更高版本上调用 Resources#getDrawable(int,theme)。它还支持Android API 4+。它不过是:

ResourceCompat.getDrawable() will call Resources#getDrawable(int, theme) on APIs 21 or greater. It also supports Android APIs 4+. It is no more than this:

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);
    }
}

Where-in ResourcesCompatApi21 只调用 res.getDrawable(id,theme)。这意味着如果设备不支持矢量绘图,它将允许绘制矢量绘图。但是,它允许你传入一个主题。

Where-in ResourcesCompatApi21 merely calls res.getDrawable(id, theme). This means it will not allow vector drawables to be drawn if the device does not support vector drawables. It will, however, allow you to pass in a theme.

同时, AppCompatResources.getDrawable(Context context,int resId)最终落到这个:

    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 +。

So this instance it will attempt to draw the resource if it can, otherwise it looks in the ContextCompat version to get the resource. Then it will even tint it if necessary. However, this method only supports API 7+.

所以我想决定是否应该使用它们,

So I guess to decide if you should use either,


  1. 您是否需要支持API 4,5或6?

  1. Do you have to support API 4, 5, or 6?


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

  • 否:继续前往#2。

您是否绝对需要提供自定义主题?

Do you absolutely need to supply a custom Theme?


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

  • 否:使用 AppCompatResources

  • Yes: No choice but to use ResourcesCompat
  • No: Use AppCompatResources

这篇关于ResourcesCompat.getDrawable()vs AppCompatResources.getDrawable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆