如何拉伸/缩放背景图片在Android线性布局?缩放、线性、布局、背景图片

2023-09-05 04:24:08 作者:情人最终为何形同陌路

我需要一些帮助,使背景图像拉伸或者两者的线性布局垂直和水平方向扩展。每次我尝试但设置背景,它不拉伸图像以适应屏幕。我不关心保持宽高比无论是。

I need some help making a background image stretch or scale both vertically and horizontally in a linear layout. Every time I try to set the background however, it doesn't stretch the image to fit the screen. I'm not concerned about keeping the aspect ratio either.

下面就是我有一个测试XML的时刻:

Here's what I've got in a test xml at the moment:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background">
</LinearLayout>

看起来像这样在preVIEW(在游戏中):

Which looks like this in preview (and in the game):

如果我改变layout_height到FILL_PARENT我得到一个重复的背景:

If I change the layout_height to fill_parent I get a repeating background:

我敢肯定,该解决方案是非常基本的,但由于某种原因,我很想念它。任何帮助将是AP preciated

I'm sure the solution is extremely basic, but for some reason I'm missing it. Any help would be appreciated

感谢

推荐答案

在这里是很重要的安卓scaleType 可能的值中的一个参数:

here is important android:scaleType parameter with one of possible values:

矩阵 fitXY fitStart fitCenter fitEnd 中心 centerCrop centerInside

如果你不介意的图像被croped在必要时(不同的比例比图像的画面),你不希望图像变形,我会建议 centerCrop 。 如果是重要的形象不会被裁剪而你不知道IND如果是歪斜的,比 fitXY 将做的工作。

if you don't mind image to be croped when it is necessary (screen of different ratio than your image) and you don't want image to be deformed, I would recommend centerCrop. if it is important that image is not cropped and you don't ind if it is skewed, than fitXY will do the job.

希望这有助于和清理了一点东西......

Hope this helped and cleared things up a bit...

 
精彩推荐
图片推荐