Android layer-list does not show scale drawable item?
Asked Answered
A

2

10

Using the layer-list below, my scale drawable is never shown. Why is that?

menu_dialog_header.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
      <solid android:color="@color/black"></solid>
    </shape>
  </item>
  <item>
    <scale
      android:scaleHeight="50%"
      android:scaleWidth="100%"
      android:scaleGravity="top"
      android:drawable="@drawable/shine" />
  </item>
</layer-list>

shine.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="@color/shine" />
</shape>

menu_dialog.xml

<!-- header -->
<LinearLayout
  android:background="@drawable/menu_dialog_header"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal|center"
  android:padding="8dp"
  android:orientation="horizontal" >
  ...
Anamorphism answered 2/12, 2011 at 3:2 Comment(0)
B
14

I have a workaround that may or may not work for you: instead of using <scale> , try <inset>. You can use a combination of the <item>'s android:top / android:bottom / android:left / android:right and the <inset>'s android:insetXXX to size the image

You can do things like set the sides of each inset value relative to your source image's dimensions. For example, if you have a 64 pixel-wide image and want to cut it down by 50%, you can do (64 / 4 = 16) for each side

Birk answered 7/6, 2013 at 18:41 Comment(4)
I don't get how this differs from a single use of item. Can you provide an example ?Gaberlunzie
@Joe you, my friend, are a king ! I had to do it programmatically in order to have a more general mechanism, so there was an issue of immutability, but this inset idea is exactly what i needed.Glebe
Funny, just getting back into Android and forgot I asked this question. I'm using this inset answer in a new project, as I don't believe it was an option back in 2011 (v2.3).Anamorphism
Definitely. Even if it was an option, sometimes it could be buried on a page somewhere, or you later find out how to use it and say "Oh! THAT's what it's for!" or "I never thought of that!" I also want to be proactive in preventing xkcd.com/979Birk
H
7

It's not exactly the LayerLists's fault. ScaleDrawable additionally depends on the drawable's level, as (briefly) mentioned both in the Drawable Resources and the ScaleDrawable reference.

It seems there is no documentation of how ScaleDrawable will interpret the level.

There seems to be no way to set a drawable's level in XML, which reflects the fact that the primary use case for ScaleDrawable is to animate a drawable's size. Hence, you'll have to set the drawable's level in your code.

I strongly sympathize with your usage of ScaleDrawable here, because it allows for a more powerful way of stacking drawables by being able to position them and define their size.

Hitherward answered 4/12, 2012 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.