ResourcesCompat.getDrawable() vs AppCompatResources.getDrawable()

后端 未结 4 1586
醉话见心
醉话见心 2021-01-30 08:45

I\'m a bit confused with these two APIs.

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

Return a drawable

4条回答
  •  悲哀的现实
    2021-01-30 09:17

    Here is my understand after some test

    ContextCompat.getDrawable(@NonNull Context context, @DrawableRes int resId)
    
    ResourcesCompat.getDrawable(@NonNull Resources res, @DrawableRes int id, @Nullable Theme theme)
    
    AppCompatResources.getDrawable(@NonNull Context context, @DrawableRes int resId)
    
    VectorDrawableCompat.create(@NonNull Resources res, @DrawableRes int resId, @Nullable Theme theme
    

    The first thing see is VectorDrawableCompat and ResourcesCompat can specific theme

    I) Without using

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in onCreated of Application class

    1) For vector image

    • API >= 21

      • ContextCompat work well
      • ResourcesCompat work well
      • AppCompatResources work well
      • VectorDrawableCompat work well
    • API < 21

      • ContextCompat crash
      • ResourcesCompat crash
      • AppCompatResources work well
      • VectorDrawableCompat work well

    2) For normal image

    • In all level
      • ContextCompat work well
      • ResourcesCompat work well
      • AppCompatResources work well
      • VectorDrawableCompat crash

    II) Using

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in onCreated of Application class

    1) For vector image

    • In all level
      • ContextCompat work well
      • ResourcesCompat work well
      • AppCompatResources work well
      • VectorDrawableCompat work well

    2) For normal image

    • In all level
      • ContextCompat work well
      • ResourcesCompat work well
      • AppCompatResources work well
      • VectorDrawableCompat crash

提交回复
热议问题