XML 格式的 Android 位图图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23079355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android bitmap image size in XML
提问by Erma Isabel
In my XML file, I'm using bitmap as the following
在我的 XML 文件中,我使用位图如下
<bitmap
android:src="@drawable/Icon"
android:gravity="center"/>
Here the image width of the icon exceeds the screen.
这里图标的图像宽度超过了屏幕。
I tried android:width="100dp" and all. But it isn't working.
我试过 android:width="100dp" 等等。但它不起作用。
Complete XML file:
完整的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<stroke android:width="3dp" android:color="@color/black" />
<solid android:color="@color/bg_green" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/Icon"
android:gravity="center"/>
</item>
</layer-list>
How can I reduce the width and height of the above bitmap?
如何减少上述位图的宽度和高度?
采纳答案by Yakiv Mospan
From Android DevelopersI saw that there is no tag for Bitmap XML that allow you to change it size. You need to do this via some Graphic Applications like GIMP. As @Kevin Cooper told already, ideally you need to create different sets of resources (with different sizes) for different screen types.
从Android Developers我看到 Bitmap XML 没有允许您更改其大小的标记。您需要通过一些图形应用程序(如 GIMP)来执行此操作。正如@Kevin Cooper 已经说过的,理想情况下,您需要为不同的屏幕类型创建不同的资源集(具有不同的大小)。
But still you can resize it using width and height attributes in ImageView
, instead of creating bitmap with different size:
但是您仍然可以使用 中的 width 和 height 属性调整它的大小ImageView
,而不是创建不同大小的位图:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/Icon" />
If you want to reuse this image in other places, you can create separate style and reuse it:
如果你想在其他地方重复使用这个图像,你可以创建单独的样式并重复使用它:
// in styles.xml
<style name="Icon24x24">
<item name="android:src">@drawable/Icon</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">24dp</item>
<item name="android:layout_height">24dp</item>
</style>
// in layout.xml
<ImageView style="@style/Icon24x24"/>
Another way of reusing, may be creating of separate layout file for one ImageView
and then including in to other layouts.
另一种重用方法是为一个布局创建单独的布局文件ImageView
,然后将其包含到其他布局中。
If you want to use this bitmap
inside of layer list, please see @Kevin Cooper answer, as he suggested it is possible to add paddings to layer list item
:
如果您想bitmap
在图层列表中使用它,请参阅@Kevin Cooper 的回答,因为他建议可以将填充添加到layer list item
:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="20dp"
android:right="20dp">
<bitmap
android:src="@drawable/Icon"
android:gravity="center" />
</item>
</layer-list>
And as @Nicolo also pointed, form API 23, it is possible to set android:width
and android:height"
to item.
正如@Nicolo 还指出的那样,形成 API 23,可以设置android:width
和android:height"
设置项目。
回答by MaxF
Just use
只需使用
<item
android:width="200dp"
android:height="200dp"
android:drawable="@drawable/splash_background"
android:gravity="center" />
instead
反而
<item
android:left="200dp"
android:right="200dp">
<bitmap
android:src="@drawable/splash_background"
android:gravity="center" />
</item>
回答by Nicolo
Although there is no width and height parameters for bitmap, you can set bitmap size using gravity and item width/height.
虽然位图没有宽度和高度参数,但您可以使用重力和项目宽度/高度来设置位图大小。
<item
android:width="230dp"
android:height="70dp">
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/screen" />
</item>
You can set item width and height and scale bitmap inside of it using gravity.
您可以使用重力设置项目的宽度和高度并在其中缩放位图。
I know this is old question but maybe someone will find it useful.
我知道这是个老问题,但也许有人会发现它很有用。
WARNING:
警告:
As already mentioned in comments, this method ONLY works with API level 23 (Android 6.0) and up.
正如评论中已经提到的,此方法仅适用于 API 级别 23(Android 6.0)及更高版本。
回答by Kevin Cooper
If you want to reduce the width and height so it fits on the screen, and you don't need to set exact dimensions, you can simply set left
and right
on the parent item. That will essentially add a margin around the image so it takes up less space:
如果您想减小宽度和高度以使其适合屏幕,并且您不需要设置确切的尺寸,您可以简单地在父项上设置left
和right
。这基本上会在图像周围添加一个边距,因此它占用的空间更少:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="20dp"
android:right="20dp">
<bitmap
android:src="@drawable/Icon"
android:gravity="center" />
</item>
</layer-list>
Ideally, though, you should upload different size images for each screen resolution (e.g. hdpi
, xxhdpi
) and then you won't need to programmatically set the size at all. You can see the full list here.
不过,理想情况下,您应该为每个屏幕分辨率(例如hdpi
、xxhdpi
)上传不同大小的图像,然后您根本不需要以编程方式设置大小。您可以在此处查看完整列表。
回答by Gilbert Arafat
this one I use for resize my mipmap on to my launch screen.
这个我用来将我的 mipmap 调整到我的启动屏幕上。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="@color/colorPrimary"/>
<item
android:width="200dp"
android:height="200dp"
android:gravity="center">
<bitmap
android:gravity="fill"
android:src="@mipmap/ic_launcher"
android:mipMap="true"/>
</item>
</layer-list>
回答by Pedro Henrique Pozzer
Try to do it this way, this worked for me:
尝试这样做,这对我有用:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="30dp"
android:height="30dp"
android:gravity="center"
>
<bitmap
android:src="@drawable/screen"/>
</item>
</layer-list>
回答by amorenew
I just resized the image manually then it works for me
我只是手动调整图像大小然后它对我有用