【发布时间】:2014-03-15 19:48:31
【问题描述】:
我正在尝试将照片设置为Activity 的背景。我希望背景是a full screen image (no borders)。
因为我希望图像填充整个活动的背景而不拉伸/挤压(即 X 和 Y 的不成比例缩放),并且我不介意是否必须裁剪照片,所以我使用 RelativeLayout一个ImageView(和android:scaleType="centerCrop")和我的布局的其余部分由一个ScrollView 和它的孩子组成。
<!-- 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 会重新调整大小。无论软键盘是否可见,我都希望背景保持不变。
我在 SO 上看到 plenty of questions 关于 ImageViews 正在重新调整大小但(imo)没有令人满意的答案。它们中的大多数只包括设置android:windowSoftInputMode="adjustPan" - 这并不总是实用的,特别是如果您希望用户能够在活动中滚动 - 或者使用不裁剪图像的getWindow().setBackgroundDrawable()。
我已经设法继承 ImageView 并覆盖其onMeasure()(请参阅我的答案:ImageView resizes when keyboard open),以便它可以强制固定高度和宽度 - 等于设备的屏幕尺寸 - 根据标志但我想知道是否有更好的方法来实现我想要的结果。
所以,总而言之,我的问题是:如何将 Activity 的背景设置为全屏照片
使用 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 并将其设置为根视图的背景即可。
【问题讨论】:
-
你能发布你的 XML 吗?这听起来像您希望活动的背景不缩放(这可能是相对布局中的 imageView 和包含其他元素的滚动视图),但是您正在覆盖 imageview,然后提到 EditText。您的 XML 可能有助于澄清问题。
-
@Jim 我已经发布了一些 XML。覆盖 ImageView 只是我考虑过的一种可能的解决方案,但如果可以的话,我宁愿避免它。
-
不要使用 ImageView,使用适当的 BitmapDrawable(顶部或中心重心)作为布局背景
-
我假设你已经尝试了这里的所有东西(我认为“isScrollContainter”解决了我的问题,但我现在找不到它):stackoverflow.com/questions/4207880/…
-
太棒了。这是最好的解决方案,下面贴一些代码供大家使用。
标签: java android android-layout android-imageview background-drawable