Support library VectorDrawable Resources$NotFoundException

前端 未结 14 1293
你的背包
你的背包 2020-11-29 18:40

I am using Design Support Library version 23.4.0. I have enabled the gradle flag:

defaultConfig {
    vectorDrawables.useSupportLibrary = tr         


        
相关标签:
14条回答
  • 2020-11-29 18:58

    Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities.

    1. Create a class which extends to Application (android.app.Application)

    public class MyApplicationClass extends Application
    {
        @Override
        public void onCreate()
        {
            super.onCreate();
        }
    }
    

    2. Head over to Manifest.xml and add the following line to your tag

    <application
        android:name=".MyApplicationClass"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
    </application>
    

    3. Add the following code above onCreate in MyApplicationClass.java

    // This flag should be set to true to enable VectorDrawable support for API < 21
    static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
    

    Complete code for MyApplicationClass.java

    import android.app.Application;
    import android.support.v7.app.AppCompatDelegate;
    
    /**
    * Created by Gaurav Lonkar on 23-Dec-17.
    */
    
    public class MyApplicationClass extends Application
    {
        // This flag should be set to true to enable VectorDrawable support for API < 21
        static
        {
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        }
    
        @Override
        public void onCreate()
        {
            super.onCreate();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:58

    Use AppCompatImageView instead of ImageView as said by Harish Gyanani in comments , it works fine with this for me.

    Official docs

    0 讨论(0)
  • 2020-11-29 18:59

    We had the same issue. Vector drawables were not visible on Kitkat. I solved this issue by adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); to the onCreate method of Activities.

    Before that dont forget to add:

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    

    and call setImageResource for the view that you use the vector drawable. My view is ImageButton. I have Android SDK build tools version 23.0.3

    0 讨论(0)
  • 2020-11-29 19:00

    Do not put your vectors in drawable-anydpi , old devices does not support that

    put them in drawable

    0 讨论(0)
  • 2020-11-29 19:02


    Inflating Drawable's

    `VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way:

    • Calling static getDrawable() methods:
    //This will only inflate a drawable with <vector> as the root element
    VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
    
    //This will only inflate a drawable with <animated-vector> as the root element
    AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
    
    // This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
    ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

    If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient.
    The library has the following morph (bi-directional) animations :

  • Play-Pause morph animation
  • Play-Stop morph animation
  • Arrow-Hamburger menu morph animation

  • As you can see, I produced the above image on my API 16 phone:

    import com.wnafee.vector.compat.AnimatedVectorDrawable;
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
    

    Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat
    This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file):

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:design:25.0.0'
    //not needed
    //  compile 'com.android.support:support-vector-drawable:25.0.0'
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
    //  Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
    //not needed
    //  compile 'com.android.support:support-animated-vector-drawable:25.0.0'
    }
    
0 讨论(0)
  • 2020-11-29 19:03

    I had a similar problem long ago, it did not work by setting

    vectorDrawables.useSupportLibrary = true

    only worked when I created the "mipmap" folder, and the code used

    imageView.setImageResource (R.mipmap.drawable_image)

    It has more Info here

    0 讨论(0)
  • 提交回复
    热议问题