DNwalter
DNwalter

Reputation: 61

set a layer-list item from SVG

I have an interesting question.

<layer-list>
        <item>
            <shape android:shape="oval">
                <size android:width="@dimen/cb_shape_size" android:height="@dimen/cb_shape_size"/>
                <solid android:color="@color/cb_unchecked_solid"/>
                <stroke android:width="@dimen/cb_shape_stroke"
                        android:color="@color/cb_checked_stroke"/>
            </shape>
        </item>
        <item android:drawable="@drawable/main_cb_gps_off"
              android:gravity="center"/>
    </layer-list>

The main_cb_gps_off is a svg file. The preview likes this in Android Studio:

enter image description here

But the view likes this when I run the project in my phone(Android4.4.2):

enter image description here

And when I run the project in the phone of Android6.0, the view is as same as the preview of Android Studio. Then at the same time, I found that:

<layer-list>
            <item android:drawable="@drawable/main_cb_gps_off"
                  android:gravity="center"/>
        </layer-list>

This code runs in Android4.4.2, the size of svg file is normal. So I don't know why the siez of svg file becomes larger when the "layer-list" has two item. I want to know. Any solution?

Upvotes: 4

Views: 8690

Answers (3)

lenooh
lenooh

Reputation: 10682

If you use drawable in layer-list, it will be scaled to fit the view it's in.

From the documentation:

All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate.

To avoid scaling, you have to use a bitmap:

To avoid scaling items in the list, use a bitmap element inside the item element to specify the drawable and define the gravity to something that does not scale, such as "center".

From: https://developer.android.com/guide/topics/resources/drawable-resource#LayerList

Upvotes: 4

Dmitry
Dmitry

Reputation: 1

I had the same problem and I used two separate ImageView in <FrameLayout> with android:layout_gravity="center"

Upvotes: 0

You can set the size inside your SVG xml file, if you declare it's size to 100dp for width and height, the layer list will be use that values to draw your icon.

If you need to increase or decrease the size, do it inside your xml file.

Upvotes: 1

Related Questions