How do items of a LayerList drawable scale to fit the container View?

Viewed 10045

From this developer guide about Layer-list drawables,

  1. 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.

    In the first sentence, they say that the items are scaled to fit the container view (and Not that the view is scaled according to the size of the items contained in it). Then they say that the size of the container view might increase (which means that the View is being scaled, right?). So doesn't the second sentence contradict the first one? Can somebody explain what is meant there?


  2. android:drawable

    Drawable resource. Required. Reference to a drawable resource.

    ...

    To avoid scaling items in the list, use a element inside the element to specify the drawable...

    ...

    For example, the following defines an item that scales to fit its container View:

    <item android:drawable="@drawable/image" /> 
    

    To avoid scaling, the following example uses a element with centered gravity:

    <item>
      <bitmap android:src="@drawable/image"
              android:gravity="center" />
    </item>
    

    Again, they say that android:drawable is a required attribute, and then they give an example which does not use this attribute. What is correct?


  3. 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"

    How is gravity scalable and how is center as its value make it unscalable?

2 Answers

In addition to what @Cristopher_Boyd said. The screen density is also important, so different bitmaps should be created for different screen densities. If there is a single image inside the drawable folder directly, it may not scale properly. Hence, generate different images, or place a single image in drawables-xxhdpi folder, for example (but not on drawables). See this answer.

Hope this helps someone,

Xavi

Related