0

I have a 24dp icon defined as a vector-drawable like this:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="100"
    android:viewportHeight="100"
    android:width="24dp"
    android:height="24dp">
    <group
        android:rotation="45"
        android:translateX="51.52484"
        android:translateY="-19.96257">
        <path
            android:pathData="M19.199993 36l19.600014 0C38.910803 36 39 36.089197 39 36.199993l0 6.75947c0 0.110796 -0.0892 0.199993 -0.199993 0.199993l-19.600014 0C19.089197 43.159456 19 43.070259 19 42.959463l0 -6.75947C19 36.089197 19.089197 36 19.199993 36Z"
            android:fillColor="#000000" />
        <path
            android:pathData="M41.2 29l16.6 0c0.1108 0 0.2 0.0892 0.2 0.2l0 18.6C58 47.9108 57.9108 48 57.8 48L41.2 48C41.0892 48 41 47.9108 41 47.8L41 29.2C41 29.0892 41.0892 29 41.2 29Z"
            android:fillColor="#000000" />
        <path
            android:pathData="M60.199993 36l19.600014 0C79.910803 36 80 36.089197 80 36.199993l0 6.75947c0 0.110796 -0.0892 0.199993 -0.199993 0.199993l-19.600014 0C60.089197 43.159456 60 43.070259 60 42.959463l0 -6.75947C60 36.089197 60.089197 36 60.199993 36Z"
            android:fillColor="#000000" />
        <path
            android:pathData="M50 50C36.859076 50 25.683178 58.456704 21.625 70.21875 28.83571 73.156161 39.090953 75 50.5 75 61.427942 75 71.317525 73.285885 78.46875 70.5625 74.508388 58.626443 63.265188 50 50 50Z"
            android:fillColor="#000000"
            android:fillAlpha="0.1568628" />
        <path
            android:pathData="M50 50c-13.140924 0 -24.316821 8.456704 -28.375 20.21875 1.462352 0.595715 3.051404 1.125957 4.75 1.625C29.757128 62.04261 39.049633 55 50 55c11.054729 0 20.418772 7.177719 23.71875 17.125 1.683691 -0.46586 3.282402 -1.003598 4.75 -1.5625C74.508387 58.626443 63.265188 50 50 50Z"
            android:fillColor="#000000" />
        <group
            android:scaleX="1.4"
            android:scaleY="1.40625"
            android:translateX="-16.5"
            android:translateY="-45.53125">
            <path
                android:pathData="M50 82.5A2.5 2.5 0 0 1 47.5 85 2.5 2.5 0 0 1 45 82.5 2.5 2.5 0 0 1 47.5 80 2.5 2.5 0 0 1 50 82.5Z"
                android:fillColor="#000000" />
        </group>
    </group>
</vector>

And I need to use it in another place in a different size (i.e. 36dp)

How can I do it without defining it twice?

I tried to use a ScaleDrawable like this:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:drawable="@drawable/icon_24dp"
       android:scaleGravity="fill"
       android:scaleHeight="150%"
       android:scaleWidth="150%" />

but it does not work.

Please, do not answer to set the size in ImageView, it's not what I'm looking for.

Thanks

ARLabs
  • 1,465
  • 1
  • 14
  • 25

2 Answers2

0

This vector drawable that you created contains all the information it needs to draw itself in any dimensions.
You don't need another drawable with different size.
You can try it in 2 ImageViews like:

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:srcCompat="@drawable/what"/>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:srcCompat="@drawable/what"/>

you can see that the drawable resizes correctly to fit the 2 different sized ImageViews.
For very large sizes you may see the drawable pixelated.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thanks but, as I stated in the question, this is not what I'm looking for. I need to use Drawable in a Preference.xml, not in an ImageView – ARLabs Oct 31 '18 at 09:10
0

If you don't want to re-define your vector drawable for each size you want to use it you will have to turn ON the compatibility support for Vector Drawables.

Please read my answer here to find the details on the changes you need to make once you turn ON the support.

** Update **

It seems that you want to put your drawables in Preferences, Im thinking that if you want to control the size of your drawable while keeping 1file you will have to try to scale it programmatically or creating your own custom Preferences view.

Benny
  • 1,650
  • 17
  • 20
  • That is not an answer to my question. – ARLabs Nov 12 '18 at 16:50
  • Hi ARLabs, turning ON the support for vector drawable will handle the resizing of your vectors on Runtime, I think this covers your question. – Benny Nov 14 '18 at 00:45
  • No, that's not the question. I need two identical drawables with 2 different intrinsic size (in dp). I'd like to define them without duplicating all path-data. – ARLabs Nov 16 '18 at 11:29
  • How different are those sizes? Im guessing they are not just a scale of the other size? – Benny Nov 16 '18 at 22:22
  • It seems that you want to put your drawables in Preferences, Im thinking that if you want to control the size of your drawable while keeping 1file you will have to try to scale it programmatically or creating your own custom Preferences view. – Benny Nov 16 '18 at 22:34