我正在尝试将照片设置为的背景Activity
。我希望背景是全屏图像(无边框)。
因为我想让图像填充整个活动的背景而不会拉伸/压缩(即X和Y的比例不成比例),而且我不介意是否必须裁剪照片,所以我使用了RelativeLayout
带ImageView
(with android:scaleType="centerCrop"
)和我的其余布局由aScrollView
及其子元素组成。
<!-- 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
视图,并且当软键盘显示时,ImageView会重新调整大小。无论软键盘是否可见,我都希望背景保持不变。
我已经看到很多关于ImageViews调整大小的问题,但是(imo)没有令人满意的答案。它们中的大多数仅包括设置android:windowSoftInputMode="adjustPan"
-并不总是实用的,特别是如果您希望用户能够滚动活动-或使用getWindow().setBackgroundDrawable()
不裁剪图像。
我设法对ImageView进行了子类化并覆盖了它onMeasure()
(请在此处查看我的答案:键盘打开时ImageView会调整大小),以便它可以根据标志强制设置固定的高度和宽度-等于设备的屏幕尺寸-但我想知道是否有更好的方法来达到我想要的结果。
因此,总而言之,我的问题是:如何将活动的背景设置为全屏照片
使用scale type =“ centerCrop”时,照片将被均匀缩放(保持其纵横比),因此的两个尺寸(宽度和高度)将等于或大于视图的相应尺寸;
弹出软键盘时不会调整大小;
回答:
我最终遵循了@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
并将其设置为根视图的背景即可。
试试这个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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句