Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompatDrawableManager.get() vs VectorDrawableCompat.create()

I am using support lib version 24.2.1, and have enabled support vectors with AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

What is the difference in these functions regarding support vectors? I was using VectorDrawableCompat.create(getResources(), R.drawable.my_vector, null). But this does not produce a drawable on my test device (Android 4.3) when setting a drawable on a button programmatically like so:

button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);

Using AppCompatDrawableManager.get().getDrawable(getActivity(), R.drawable.my_vector); (wrapped in a state list selector) seems to work okay, although I can't seem to find the documentation for it.

like image 892
Daniel Wilson Avatar asked Oct 25 '16 13:10

Daniel Wilson


1 Answers

What is the difference in these functions regarding support vectors?

AppCompatResources.getDrawable(...) will inflate all kinds of drawables including

  • vector drawables below API 21 (only if support vector drawables are enabled by build script; read further)
  • properly themed AppCompat drawables
  • any other drawable obtainable by ContextCompat.getDrawable(Context, int)

This method internally calls AppCompatDrawableManager.get().getDrawable(Context, int), which is not part of public API. Both methods equal in function from consumer point of view.

VectorDrawableCompat.create(...) will only inflate vector drawables (only if support vector drawables are enabled by build script; read further).

But this does not produce a drawable on my test device (Android 4.3)

VectorDrawableCompat.create(...) will return null on error. This can happen if the referenced drawable is not a vector drawable which in turn can happen if you didn't configure the build plugin correctly and PNGs are generated for platforms below API 21.

Activate vector drawables support below API 21 by altering app module build.gradle:

// Gradle Plugin 2.0+  
android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
  }  
}  

See Android Support Library 23.2 blog post for further info.

I am using support lib version 24.2.1, and have enabled support vectors with AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

This method does not "enable support vectors". First of all you need to enable support vector drawables in build.gradle as said above.

After that this method basically enables support vector drawables inside drawable containers like LayerDrawable or StateListDrawable.

See AppCompat — Age of the vectors for more info how this can be abused.

like image 87
Eugen Pechanec Avatar answered Nov 18 '22 08:11

Eugen Pechanec