Android 缩放图像,使其在背景中的纵横比可绘制
当使用Android 缩放图像,使其在背景中的纵横比可绘制,android,scaling,android-drawable,android-bitmap,android-background,Android,Scaling,Android Drawable,Android Bitmap,Android Background,当使用作为背景可绘制XML时,如何使背景图像适合视图,但保持其纵横比? 的android:gravity值均未达到预期效果。仅在xml文件中操作背景属性是不可能的。有两种选择: 您可以使用编程方式剪切/缩放位图 并将其设置为某个视图的背景 您可以使用ImageView而不是background将其作为第一个布局的元素,并为其指定属性: <RelativeLayout android:layout_width="fill_parent" android:layout_heig
作为背景可绘制XML时,如何使背景图像适合视图,但保持其纵横比?
的android:gravity值均未达到预期效果。仅在xml文件中操作背景属性是不可能的。有两种选择:
您可以使用编程方式剪切/缩放位图
并将其设置为某个视图
的背景
您可以使用ImageView
而不是background将其作为第一个布局的元素,并为其指定属性:
<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>
...
在这里放置布局组件
...
另一种方法是创建你的常规图像,并让它按你想要的方式伸缩
你可以在角落里放上9个补丁点,使内容居中,这样可以明显地保持你的比例(假设你的图像最外面的边缘是可重复/透明的)
希望您能理解。使用a.ch描述的方法对我来说非常有用,除了我使用了这种刻度类型,它对我所需要的东西更有效:
android:scaleType="centerCrop"
以下是可用比例类型的完整列表:
有一种简单的方法可以从可拉拔的屏幕上执行此操作:
您的_drawable.xml
<?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>
唯一的缺点是,如果没有足够的空间,您的图像将无法完全显示,但它将被剪裁,我无法找到一种直接从绘图板上执行此操作的方法。但是从我做的测试来看,它工作得很好,并且没有剪辑太多的图像。你可以更多地使用重力选项
另一种方法是创建一个布局,在这里您将使用图像视图
,并将
设置为fitCenter
希望这些信息能帮助您实现您的目标。尝试使用InsertDrawable(对我来说效果很好)
只要给这个你的可拉伸,和插入(或填充),你想从四个方面之一
InsetDrawable insetDrawable = new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom);
它专门用于设置背景可绘制,大小小于或小于视图
请看这里:
这是一个老问题,但其他答案都不适合我。但是,此xml代码没有:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/background_image"/>
</RelativeLayout>
如果位图的宽度大于其高度,请使用android:gravity=“fill\u vertical”
。否则,请使用android:gravity=“fill\u horizontal”。这与在ImageView
上使用android:scaleType=“centerCrop”
的效果类似
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="fill_vertical"
android:src="@drawable/image" />
如果支持多个方向,可以在
可绘制端口
文件夹中创建一个位图XML文件,在可绘制区域
文件夹中创建另一个位图XML文件。我想在自定义可绘制类中执行类似操作。
以下是重要的部分:
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);
}
}
}
}
通过这种方法,您可以灵活地应用所需的任何缩放逻辑,以便将图像适配到可用空间(或者如果您在dp中设置了宽度和高度),如果图像不太宽,我也尝试了以下方法 在这里,我为方形图像设置了相同的宽度和高度[或者您可以
将内容
包装在两者上]
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/background_image"/>
</RelativeLayout>
adjust view bounds
和scale type center fit
起作用。您已经尝试过了吗?试试android:scaleType=“matrix”scaleType用于ImageView。位图不支持它。所有可能的android:scaleType
值都不会提供所需的效果。只有在我根本没有指定scaleType
的情况下,图像才适合视图,并保持其纵横比。在这里,您可以看到每种类型的一些示例:不幸的是,它不适用于android:background
。这会保持图像不缩放和较小,并且只在底部显示。事实上,如果您需要缩放它,其他解决方案更合适。如您所见,这些属性不涉及任何类型的缩放。这并不能回答问题。