在 xml 中调整位图中的图像大小 [英] Resize images in bitmap in xml

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

问题描述

我是 Android 编程的新手.最近,我开始了一个项目,想为 Android 应用程序制作一个启动画面.在

以下是我的background_splash.xml:

<项目><位图机器人:重力=中心"android:src="@drawable/tap"/></项目></层列表>

我试过了

android:width="75sp"机器人:高度=75sp"

中,但它不起作用.因此,我想知道除了使用 ImageView 之外,是否有一种方法可以调整图像大小(最好不使用 Java 代码).任何帮助将不胜感激!

解决方案

我在为我的应用设计启动画面时遇到了同样的问题(按照相同的链接你在上面和其他一些人提到过),通过谷歌搜索得到了我的答案.

解决方案很简单,不涉及编码. 即.您不需要在 xml 中指定高度和宽度.至于为什么会发生这种情况,您的图像分辨率高于比下面指定的要高.

解决方案:

您需要提供映射到各种屏幕尺寸的各种分辨率的图像.以下是 Google 提供的各种显示器的最小屏幕尺寸列表(均以像素为单位):-

<小时>

对于 Android 移动设备

LDPI- 426 x 320

MDPI- 470 x 320

HDPI- 640 x 480

XHDPI- 960 x 720

<小时>

对于 Android 平板设备

LDPI- 200 x 320

MDPI- 320 x 480

HDPI- 480 x 800

XHDPI- 720 x 1280

<小时>

所以您需要做的就是调整大小所有尺寸的图片,加载 Drawable 中的图像根据它们的大小(例如:drawable-hdpi 包含 hdpi 图像)并将其传递给 background_splash.xml(当然你可以保持相同的名称).重建项目并运行它.应符合您的规范.

注意:代码根本不需要更改.要调整图像大小,您可以使用 Adob​​e Photoshop(我发现它更易于使用).

根据喜好,您也可以使用9 Patch image,这样图片的边框可以拉伸以适应屏幕的大小,而不会影响图片的静态区域.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

I am new to Android programming. Recently, I had started on a project and would like to make a splash screen for the Android app. I had read and successfully implemented the splash screen after following this tutorial. However, I realise that my app logo stretches out of my screen, like this:

The following is my background_splash.xml:

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

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/tap" />
    </item>
</layer-list>

I have tried

android:width="75sp"
android:height="75sp"

in <bitmap> but it does not work. Thus, I would like to know if there is a way to resize the image (without Java code, preferably) except for using an ImageView. Any help would be appreciated!

解决方案

I faced the ditto same problem while designing splash screen for my app (followed the same link that you mentioned above and a few others) , and a bit of googling got me my answer.

The solution is simple and does not involve coding. ie. you do not need to specify height and width in your xml. As for why this is happening, your image resolution is higher than as specified below.

SOLUTION :

You need to provide image of various resolutions that map to various screen sizes. Following is a list of minimum screen size for various displays as provided by Google (all in pixels) :-


For Android Mobile Devices

LDPI- 426 x 320

MDPI- 470 x 320

HDPI- 640 x 480

XHDPI- 960 x 720


For Android Tablet Devices

LDPI- 200 x 320

MDPI- 320 x 480

HDPI- 480 x 800

XHDPI- 720 x 1280


So all you need to do is resize your image for all sizes, load the images in Drawable according to their sizes (Ex: drawable-hdpi contains the hdpi image) and pass it to the background_splash.xml (you can keep the same name ofcourse). Rebuild the project and run it. Should be as per your specification.

Note: Code does not need to be changed at all. For image resizing, you can use Adobe Photoshop (I find it easier to work with).

According to preference, you may also use 9 Patch image so that the image's border can stretch to fit the size of the screen without affecting the static area of the image.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

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

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