gpt4 book ai didi

java - 如何拥有不调整大小的全屏背景图像(带中心裁剪)

转载 作者:IT老高 更新时间:2023-10-28 23:25:05 26 4
gpt4 key购买 nike

我正在尝试将照片设置为 Activity 的背景。我希望背景是 a full screen image (no borders) .

由于我希望图像在不拉伸(stretch)/挤压的情况下填充整个 Activity 的背景(即 X 和 Y 的不成比例缩放)并且我不介意是否必须裁剪照片,所以我使用的是 RelativeLayout 带有 ImageView(带有 android:scaleType="centerCrop")和我的布局的其余部分由 ScrollView 及其 children 。

<!-- Tried this with a FrameLayout as well... -->
<RelativeLayout>
<!-- Background -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<!-- Form -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView>
<LinearLayout>
...
<EditText/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>

问题是布局的其余部分有一些 EditText View ,当软键盘出现时,ImageView 会重新调整大小。无论软键盘是否可见,我都希望背景保持不变。

我看过plenty of questions关于 ImageViews 正在重新调整大小但(imo)没有令人满意的答案。其中大多数只包括设置 android:windowSoftInputMode="adjustPan" - 这并不总是实用的,特别是如果您希望用户能够在 Activity 中滚动 - 或使用 getWindow ().setBackgroundDrawable() 不裁剪图像。

我已经设法将 ImageView 子类化并覆盖其 onMeasure()(请参阅我的答案:ImageView resizes when keyboard open),以便它可以强制固定高度和宽度 - 等于设备的屏幕尺寸 - 根据标志,但我想知道是否有更好的方法来实现我想要的结果。

所以,总而言之,我的问题是:如何将 Activity 的背景设置为全屏照片

  • 使用 scale type = "centerCrop",使照片均匀缩放(保持其纵横比),因此两个尺寸(宽度和高度)将等于或大于相应尺寸 View ;

  • 在弹出软键盘时不会调整大小;


回答:

我最终关注了 @pskink的建议和子类 BitmapDrawable (见下面他的回答)。我必须进行一些调整以确保 BackgroundBitmapDrawable 始终以填充屏幕的方式进行缩放和裁剪。

这是我的最后一个类,改编自他的回答:

public class BackgroundBitmapDrawable extends BitmapDrawable {
private Matrix mMatrix = new Matrix();
private int moldHeight;
boolean simpleMapping = false;

public BackgroundBitmapDrawable(Resources res, Bitmap bitmap) {
super(res, bitmap);
}

@Override
protected void onBoundsChange(Rect bounds) {
if (bounds.height() > moldHeight) {
moldHeight = bounds.height();
Bitmap b = getBitmap();
RectF src = new RectF(0, 0, b.getWidth(), b.getHeight());
RectF dst;

if (simpleMapping) {
dst = new RectF(bounds);
mMatrix.setRectToRect(src, dst, ScaleToFit.CENTER);
} else {
// Full Screen Image -> Always scale and center-crop in order to fill the screen
float dwidth = src.width();
float dheight = src.height();

float vwidth = bounds.width();
float vheight = bounds.height();

float scale;
float dx = 0, dy = 0;

if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}

mMatrix.setScale(scale, scale);
mMatrix.postTranslate(dx, dy);

}
}
}

@Override
public void draw(Canvas canvas) {
canvas.drawColor(0xaa00ff00);
canvas.drawBitmap(getBitmap(), mMatrix, null);
}
}

然后只需创建一个 BackgroundBitmapDrawable 并将其设置为根 View 的背景即可。

最佳答案

试试这个 LayerDrawable (res/drawable/backlayer.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape>
<solid android:color="#f00" />
</shape>
</item>
<item>
<bitmap
android:gravity="top" android:src="@drawable/back1">
</bitmap>
</item>
</layer-list>

并将其设置为您的顶级布局:android:background="@drawable/backlayer"

更新:试试这个 BitmapDrawadle,将其设置为顶级布局 (setBackgroundDrawable()),如果 simpleMapping == true 足够好,您可以删除“else”分支:

class D extends BitmapDrawable {
private Matrix mMatrix = new Matrix();
private int moldHeight;

public D(Resources res, Bitmap bitmap) {
super(res, bitmap);
}

@Override
protected void onBoundsChange(Rect bounds) {
if (bounds.height() > moldHeight) {
moldHeight = bounds.height();
Bitmap b = getBitmap();
RectF src = new RectF(0, 0, b.getWidth(), b.getHeight());
RectF dst;

// if simpleMapping is good enough then remove "else" branch and
// declare "dst" as:
// RectF dst = new RectF(bounds);
boolean simpleMapping = true;
if (simpleMapping) {
dst = new RectF(bounds);
} else {
float x = bounds.exactCenterX();
dst = new RectF(x, 0, x, bounds.height());
float scale = bounds.height() / src.height();
float dx = scale * src.width() / 2;
dst.inset(-dx, 0);
}
mMatrix.setRectToRect(src, dst, ScaleToFit.CENTER);
}
}

@Override
public void draw(Canvas canvas) {
canvas.drawColor(0xaa00ff00);
canvas.drawBitmap(getBitmap(), mMatrix, null);
}
}

关于java - 如何拥有不调整大小的全屏背景图像(带中心裁剪),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com