7

VectorDrawableCompat finally made its way into support library in SDK 23. But it doesn't seem to work, or maybe I'm missing something.

drawable/mypic.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
    android:name="rotationGroup"
    android:pivotX="300.0"
    android:pivotY="300.0"
    android:rotation="45.0" >
    <path
        android:name="v"
        android:fillColor="#000000"
        android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>

fragment_bla.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/mypic"
    android:scaleType="centerCrop" />

</FrameLayout>

This code works in Lollipop, but not in KitKat. What am I missing?


Exception:

09-06 14:26:53.637  31820-31820/com.bla E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bla, PID: 31560
android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageView
        at android.view.LayoutInflater.createView(LayoutInflater.java:623)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
        at com.hohero.fragment.Intro1Fragment.onCreateView(Intro1Fragment.java:14)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
wiradikusuma
  • 1,930
  • 4
  • 28
  • 44
  • is there a `caused by` line after whatever you have posted in the logcat? – Bhargav Sep 06 '15 at 06:48
  • are you sure it comes to the support library? I cannot find it in the changelog of support library – Derek Fung Sep 06 '15 at 07:03
  • @Bhargav nope, the rest is just low-level Android stuff – wiradikusuma Sep 07 '15 at 07:47
  • http://developer.android.com/tools/support-library/index.html , maybe I have really missed it, can you point me to the line about that? – Derek Fung Sep 07 '15 at 07:50
  • @DerekFung not sure why it's not mentioned, like they don't want people to use it :D I knew about it from http://stackoverflow.com/a/29943417/193550 but the best way to see it yourself is by downloading 23.0.1 from SDK Manager, then Ctrl+N for `VectorDrawableCompat` in Android Studio :) – wiradikusuma Sep 07 '15 at 07:54
  • Nah man... since half a year i have heard this rumor going around. https://android.googlesource.com/platform/frameworks/support/+/master/v7/vectordrawable/ It IS there... usage.. i dont know myself. Maybe you have to reference the appcompat xml tag ` – Tom Siwik Sep 09 '15 at 23:26

2 Answers2

24

I believe the problem is android:src="@drawable/mypic" in your imageView setup.

Change that to app:srcCompat="@drawable/mypic"

Then you should be good to go.

More details, please refer to: https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.mvuq06x1k

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Tenghui Zhu
  • 309
  • 2
  • 3
2

Well, I read the code and got it kind of working. There are a couple of caveats:

  1. The xml file must be in the raw directory, not drawable
  2. The xml must be very simple. It looks like it supports a single <path> element

For me it always has a red background and scaling doesn't seem to work right. I just used the Vector Asset Action -> (lock) from the Studio Collection.

    final Drawable drawable = VectorDrawableCompat.createFromResource(getResources(), R.raw.ic_lock_outline_24dp);
    imageView.setImageDrawable(drawable);

It's a good start and something to fun to play with!

Link to Google source in an unknown branch: https://android.googlesource.com/platform/frameworks/support/+/d573775216408a88e796cd74abf8ab018488c2a3/v7/vectordrawable/src/android/support/v7/graphics/drawable

Dustin
  • 2,064
  • 1
  • 16
  • 12