Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layer-list does not show scale drawable item?

Tags:

android

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" >
  ...
like image 853
worked Avatar asked Dec 02 '11 03:12

worked


2 Answers

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

like image 113
Joe Plante Avatar answered Nov 13 '22 08:11

Joe Plante


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.

like image 38
class stacker Avatar answered Nov 13 '22 08:11

class stacker