在XML中调整位图图像大小

14

我刚开始学习Android编程。最近,我开始了一个项目,并希望为Android应用程序制作一个闪屏屏幕。我阅读了并成功地实现了这个闪屏屏幕,方法是在遵循此教程。然而,我发现我的应用程序标志拉伸出了屏幕,像这样:

Stretched out app logo

以下是我的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>

我尝试过

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

我试过在<bitmap>中调整大小,但没有成功。所以,我想知道是否有一种方法可以调整图像的大小(最好不用Java代码),除了使用 ImageView 。 谢谢!

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!


android:scaleType="fitXY",试试看... 并将您的ImageView设置为android:layout_width="match_parent"和android:layout_height="match_parent",我认为它会生效。 - Nandkishor mewara
请设置您的图层列表的高度和宽度... - Nandkishor mewara
@nandkishormewara 我会尝试一下,看看它是否有效 ☺ - user5404864
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); 尝试一下。 - Nandkishor mewara
2
你解决了这个问题吗? - Nandkishor mewara
可能是 https://dev59.com/mWgv5IYBdhLWcg3wSe-2#46115699 的重复问题。 - Davide Cannizzo
8个回答

10
我在设计应用程序的启动画面时遇到了同样的问题(按照你上面提到的链接和其他一些链接进行设计),通过谷歌搜索解决了问题。解决方案很简单,不涉及编码,也就是说,在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(当然可以保持相同名称)。重新构建项目并运行它。应该和你的规格一样。
注意:代码根本不需要改变。有关图像缩放,您可以使用Adobe Photoshop(我发现这更容易使用)。

如果需要,您也可以使用9 Patch图片,这样图片的边框就可以拉伸以适应屏幕的大小,而不会影响图片的静态区域。

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


7

只需要改变

android:gravity="center"

To

android:gravity="fill"

最终结果是什么?这是否通过保留空白来适应宽度/高度,或者实际上进行了裁剪或拉伸图像? - Bronx
2
填充会拉伸图像。 - Raj

3

Bitmap不允许使用gravity="center"调整图片尺寸,并需要为启动画面单独提供具有特定大小的图标。


3

尝试使用item代替bitmap。

Bitmap没有更改大小的选项。

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

    <item android:drawable="@android:color/white"/> 

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

</layer-list>

这需要API 23及以上版本。 - Kaaveh Mohamedi

1

有更简单的方法可以做到这一点。在您的launch_background.xml文件中,删除那个位图标签,只需像这样添加它

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

0

你是否正在使用 ImageView,尝试一下吧,我认为它会对你有所帮助...

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);

或者 检查它

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

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

3
不,我没有使用ImageView。我直接使用位图来显示它,没有使用imageView。 - user5404864

0

尝试这个,不要使用drawable

如果你想改变背景颜色,请将其赋给相对布局, 如果你想要全屏图像(最适合启动画面),请在ImageView中使用match_parent。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_centerInParent="true"
        android:src="@drawable/splash" />

</RelativeLayout>

-3

如果您想调整项目中的位图大小,不应使用位图。直接使用项目更改尺寸,例如:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/splash_background"/>
    <item
        android:src="@drawable/tap"
        android:gravity="center"
        android:width="150dp"
        android:height="150dp"/>
    </item>
</layer-list>

位图不允许使用重力 =“center”调整图像尺寸。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接