a-student/BetterVectorDrawable

Why my sPreloadedDrawables size not change?

Closed this issue · 7 comments

from your README.md

There is an overload of the first method with a boolean parameter, which allows to force system handling of vector drawables on Android 5.0+

so this my code

VectorDrawableCompat.enableResourceInterceptionFor(true,getResources(),R.drawable.airplane_vector,R.drawable.bicycle_vector);

        Class<?> cls = null;
        try {
            cls = Class.forName("android.content.res.Resources");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String name = "sPreloadedDrawables";
        Field field = null;
        try {
            field = cls.getDeclaredField(name);
            field.setAccessible(true);

            @SuppressWarnings("unchecked")
            LongSparseArray<Drawable.ConstantState>[] objField = (LongSparseArray<Drawable.ConstantState>[]) field.get(resources);

                System.out.println(objField[0].size());
                System.out.println("------------------------");
                System.out.println(objField[1].size());

sPreloadedDrawables size never change What Is Wrong?? thk

Resources.sPreloadedDrawables[x].size() is not affected by the lib.
Why do you need that? We could implement it if you really need such feature.

Note that sPreloadedDrawables is not changed at all if you run the code on Android 5.0+ and force system handling of vector drawables.

I make a mistake ( sPreloadedDrawables is different in 4.0+ ,5.0+)

I use phone on 4.1.1

sPreloadedDrawables type (LongSparseArray<Drawable.ConstantState>)

my code

VectorDrawableCompat.enableResourceInterceptionFor(false,getResources(),     
R.drawable.lamp_vector,
R.drawable.airplane_vector,
R.drawable.bicycle_vector,
R.drawable.rocket_vector);

after that sPreloadedDrawables type (LongSparseArrayDrawableConstantStateWrapper)

now i just want know size or length of sPreloadedDrawables (I think if size=4 , that means Reflection injection success and ResourceInterception success)

LongSparseArrayDrawableConstantStateWrapper objField = (LongSparseArrayDrawableConstantStateWrapper) field.get(null);
System.out.println(objField.size());

but the the result is 0 :( what's my problem??
ps:I think your gays job great maybe we will use in our app.

Imagine there is a method

boolean VectorDrawableCompat.isResourceInterceptionEnabled()

which lets you know whether resource interception was successfully enabled or not.
Would it be a good solution for you?

that's will be great 👍

1.if i enable resource interception , sPreloadedDrawables will be change ??(as you see i try to use xxx.size but result is never change . In 5.0+ the result is 390,in 4.0+ the result is 0)

2.is there any way i can check inside sPreloadedDrawables (the method that you imagine is good and easier for user,I just want a deeper understanding of 'use sPreloadedDrawables to resource interception')

Thank you for your patience.

  1. sPreloadedDrawables will be changed, but not its size. It is not good idea to increase the size when intercepting, because vector drawables are not really there (for example, one cannot remove them or enumerate).
  2. No. Could you explain what you exactly need? Doesn't the proposed method solve your problem?

We added the proposed method:

int[] ids = VectorDrawableCompat.findAllVectorResourceIdsSlow(getResources(), R.drawable.class);
VectorDrawableCompat.enableResourceInterceptionFor(getResources(), ids);

if (BuildConfig.DEBUG && !VectorDrawableCompat.isResourceInterceptionEnabled()) {
    throw new AssertionError();
}

Please, update dependency in build.gradle to be able to use the new method:

dependencies {
    …
    compile 'com.bettervectordrawable:lib:0.5+'
}

Sorry I'm so busy
I have put this library into the project and Improve it
we will update to 0.5 💯
Today is Mid-Autumn Festival in our Country,I hope you have nice day too :)