LayerList 可绘制对象的项目如何缩放以适合容器视图?

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

由此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.

    在第一句话中,他们说项目被缩放以适应 容器视图(并不是说视图是根据 其中包含的项目的大小)。然后他们说 容器视图可能会增加(这意味着视图是 正在缩放,对吧?)。 所以第二句不矛盾吗 第一个?有人可以解释一下那里的意思吗?


  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>
    

    同样,他们说 android:drawable 必需的 属性,然后他们给出了一个不使用此属性的示例。什么是正确的?


  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"

    gravity 的可扩展性如何?center 的值如何使其不可扩展?

  1. 似乎层列表项在 X 和 Y 维度上都有效地拉伸以适合容器。

  2. 每个项目都需要一个可绘制对象。位图子元素实际上成为该项目的可绘制对象。

    如果你有一个空项目(没有可绘制资源),当你尝试加载图层列表时会导致错误。

  3. Gravity 有几个选项(例如 FILL,这可能是项目的默认重力,或 FILL_HORIZONTAL 拉伸项目以填充其容器)。

    此外,您可以在项目标签本身上设置 android:gravity="center"(没有 <bitmap> 子标签),它似乎具有相同的效果(至少在 API 23 中) .

除了@Cristopher_Boyd说的。屏幕密度也很重要,因此应为不同的屏幕密度创建不同的位图。如果直接在 drawable 文件夹中只有一张图片,它可能无法正确缩放。因此,例如,生成不同的图像,或将单个图像放在 drawables-xxhdpi 文件夹中(但不放在 drawables 上)。看到这个 answer.

希望这对某人有所帮助,

哈维