使用图层列表显示一些可绘制的图像

ant*_*009 27 android drawable layer-list android-drawable genymotion

Android studio 2.0 Preview 3b

你好,

我创建了以下布局,我想用它作为我的应用程序的背景.我正在使用layer-list,我想在两个地方展示一碗豌豆.在预览中一切看起来都不错,但是当我在genymotion或一些廉价的中国设备上运行时,图像会在屏幕上延伸.但是,在Android AVD上,一切看起来都不错,在我的Nexus 5(真实设备)上一切正常.

这就是我想要的,这就是它在AVD和Nexus 5中的显示方式.你可以看到没有问题.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:centerX="0.5"
                android:centerY="0.3"
                android:endColor="#08e25b"
                android:gradientRadius="300dp"
                android:startColor="#b7e9c9"
                android:type="radial" />
        </shape>
    </item>

    <item
        android:width="48dp"
        android:height="48dp"
        android:left="350dp"
        android:top="400dp">
        <bitmap android:src="@drawable/peas" />
    </item>

    <item
        android:width="68dp"
        android:height="68dp"
        android:drawable="@drawable/peas"
        android:left="-20dp"
        android:top="480dp" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我已将peas.png文件放入drawable-nodpi,只需添加宽度和高度layer-list

当我运行genymotion和一些便宜的智能设备时,我得到以下内容: 在此输入图像描述

Just quick summary.

Nexus 5 real device and AVD devices works ok
Genymotion and cheap smart devices doesn't display correctly
Run Code Online (Sandbox Code Playgroud)

我对我应该相信的事感到困惑.我也尝试使用位图来查看是否会产生任何影响.

非常感谢任何建议.

Sac*_*inS 35

按如下方式更新图层列表

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape>
        <gradient
            android:centerX="0.5"
            android:centerY="0.1"
            android:endColor="#08e25b"
            android:gradientRadius="300dp"
            android:startColor="#b7e9c9"
            android:type="radial" />
    </shape>
</item>
<item
    android:width="48dp"
    android:height="48dp"
    android:bottom="68dp"
    android:right="-20dp">
    <bitmap
        android:gravity="bottom|right"
        android:src="@drawable/peas" />
</item>
<item
    android:height="68dp"
    android:left="-20dp"
    android:bottom="-20dp"
    android:width="68dp">
    <bitmap
        android:gravity="bottom|left"
        android:src="@drawable/peas" />
</item>
Run Code Online (Sandbox Code Playgroud)

  • 改变图像的大小,你必须把针对不同屏幕密度不同的位图资源(LDPI(低),于mdpi(中),华电国际(高),xhdpi超高),xxhdpi(超超高)和xxxhdpi) (3认同)
  • <bitmap>中的"android:gravity"表示如果位图小于容器,将drawable放在其容器中的位置. (2认同)
  • 免责声明:宽度和高度是 API 23+ (2认同)

Akh*_*mar 10

将图像放在所有密度文件夹中(xxhdpi,xhdpi,hdpi).

系统根据屏幕分辨率选择图像资源.

  • 那么这个应该标记为已接受的答案. (3认同)