在 xml 中调整位图的大小 [英] Resizing a bitmap in xml

查看:38
本文介绍了在 xml 中调整位图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的启动画面,我使用了一种样式,其可绘制对象定义为 xml.我正在使用位图来包含图片.

For my splash screen I use a style with has a drawable defined as xml. There I'm using a bitmap to include the picture.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/white"/>
  </item>
  <item
    <bitmap
        android:src="@drawable/splash_logo"
        android:gravity="center"/>
  </item>

</layer-list>

图片大小为 1024x390,我无法将其缩小.如何在 xml 中居中和缩放它以使其居中并适合屏幕?

The picture has a size of 1024x390 and I have no option to scale it down. How can I center and scale it in the xml to be in center and fit into the screen?

我尝试了很多东西,比如设置父项的宽度和高度,位图应该填充父项或设置填充,但我所有的方法都不好.我还阅读了有关此主题的其他一些 stackoverflow 答案,但没有一个解决方案适合我.

I've tried a lot of things, like setting width and height of parent item and bitmap should fill the parent or setting padding but all my approaches weren't good. I also have read some other stackoverflow answers to this topic but none of the solutions worked for me.

我可以尝试将图像缩放到适合屏幕并居中的任何其他解决方案吗?

Are there any other solutions I can try to scale the image to that it fits the screen and is centered?

推荐答案

这种方法需要更高的 API 级别(也许是 23?),但它完全可以满足您的需求.

This approach requires a higher API level (23 maybe?) but it'll do exactly what you want.

<item
    android:height="200dp"
    android:width="200dp"
    android:drawable="@drawable/splash_logo"
    android:gravity="center">
</item>

根据需要编辑宽度/高度.

Edit the width/height to be whatever you need.

这篇关于在 xml 中调整位图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆