如何使用<bitmap />
作为背景可绘制XML,如何使背景图像适合视图,但保持其长宽比?<bitmap>
的android:gravity
值均未达到所需的效果。
仅在xml文件中不可能实现操纵背景属性。有两个选项:
您可以通过编程方式切割/缩放位图Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
boolean filter)
并将其设置为某些Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
boolean filter)
的背景。
您使用View
而不是将其放置为第一个布局的元素的背景,并为其指定ImageView
属性:
android:scaleType
有一种简单的方法可以从可绘制对象中进行此操作:
your_drawable.xml
android:scaleType
唯一的缺点是,如果没有足够的空间,您的图像将无法完全显示,但是会被剪切,我找不到直接从可绘制对象中执行此操作的方法。但是从我的测试来看,它工作得很好,并且不会裁剪太多图像。您可以使用重力选项玩更多游戏。
另一种方法是只创建一个布局,您将在其中使用<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/backgrnd"
android:scaleType="centerCrop" />
...
rest layout components here
...
</RelativeLayout>
并将<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@color/bg_color"/>
<item>
<bitmap
android:gravity="center|bottom|clip_vertical"
android:src="@drawable/your_image" />
</item>
</layer-list>
设置为ImageView
。
希望此信息有助于您实现所需的目标。
另一种方法是创建常规图像的scaleType
,并按照您想要的方式拉伸比例。
您可以通过在拐角处放置9个补丁点来使内容居中,从而明显保留比例(假设图像的最外边缘是可重复/透明的。)
希望您能想到这个主意。
我想在自定义Drawable类中做类似的事情。以下是重要的片段:
fitCenter
使用这种方法,您可以灵活地应用所需的任何比例逻辑
使用a.ch描述的方法对我来说非常有用,除了我使用的这种比例类型对我需要的效果更好:
patch 9 images
这里是可用的秤类型的完整列表:public class CustomBackgroundDrawable extends Drawable
{
private Rect mTempRect = new Rect();
private Paint mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
...
public void draw(@NonNull Canvas canvas)
{
Rect bounds = getBounds();
if (mBitmap != null ) {
if (mScaleType == ScaleType.SCALE_FILL) {
//bitmap scales to fill the whole bounds area (bitmap can be cropped)
if (bounds.height() > 0 && bounds.height() > 0) {
float scale = Math.min(mBitmap.getWidth()/(float)bounds.width(), mBitmap.getHeight()/(float)bounds.height());
float bitmapVisibleWidth = scale * bounds.width();
float bitmapVisibleHeight = scale * bounds.height();
mTempRect.set((int)(mBitmap.getWidth()-bitmapVisibleWidth)/2, 0, (int)(bitmapVisibleWidth+mBitmap.getWidth())/2, (int)bitmapVisibleHeight);
canvas.drawBitmap(mBitmap, mTempRect, bounds, mBitmapPaint);
}
} else if (mScaleType == ScaleType.SCALE_FIT) {
//bitmap scales to fit in bounds area
if (bounds.height() > 0 && bounds.height() > 0) {
float scale = Math.min((float)bounds.width()/mBitmap.getWidth(), (float)bounds.height()/mBitmap.getHeight());
float bitmapScaledWidth = scale * mBitmap.getWidth();
float bitmapScaledHeight = scale * mBitmap.getHeight();
int centerPadding = (int)(bounds.width()-bitmapScaledWidth)/2;
mTempRect.set(bounds.left + centerPadding, bounds.top, bounds.right - centerPadding, bounds.top+(int)bitmapScaledHeight);
canvas.drawBitmap(mBitmap, null, mTempRect, mBitmapPaint);
}
}
}
}
尝试使用InsetDrawable(对我来说效果很好)。
只需为此提供您的可绘制对象,以及您希望从四个侧面中的任何一个插入或填充)。>
android:scaleType="centerCrop"
它专门用于设置可绘制的背景,尺寸小于或小于视图。
请参见此处:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
如果您的位图宽于高,请使用InsetDrawable insetDrawable = new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom);
。否则,请使用https://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html。这与在android:gravity="fill_vertical"
上使用android:gravity="fill_horizontal"
具有相似的效果。
[旧问题,但其他答案都对我无济于事。但是此xml代码执行了以下操作:
为了使图像[[适合