android 背景被放大,android-缩放图像以使其背景纵横比保持其纵横比

android-缩放图像以使其背景纵横比保持其纵横比

使用作为背景可绘制XML时,如何使背景图像适合视图但保持其纵横比?的android:gravity的值均未达到所需的效果。

9个解决方案

55 votes

仅在xml文件中实现操纵background属性是不可能的。 有两种选择:

您可以使用以下方式以编程方式剪切/缩放位图

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/backgrnd"

android:scaleType="centerCrop" />

...

rest layout components here

...

,并将其设置为

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/backgrnd"

android:scaleType="centerCrop" />

...

rest layout components here

...

的背景。

您使用

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/backgrnd"

android:scaleType="centerCrop" />

...

rest layout components here

...

而不是将其作为第一个布局的元素放置在背景上,并为其指定

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/backgrnd"

android:scaleType="centerCrop" />

...

rest layout components here

...

属性:

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/backgrnd"

android:scaleType="centerCrop" />

...

rest layout components here

...

a.ch. answered 2019-10-16T01:14:25Z

33 votes

有一个简单的方法可以从drawable中做到这一点:

your_drawable.xml

android:gravity="center|bottom|clip_vertical"

android:src="@drawable/your_image" />

唯一的缺点是,如果没有足够的空间,您的图像将无法完全显示,但是会被剪切,我找不到直接从可绘制对象中进行此操作的方法。 但是从我做的测试来看,它工作得很好,并且不会裁剪太多的图像。 您可以使用重力选项玩更多游戏。

另一种方法是只创建一个布局,在这里您将使用fitCenter,并将fitCenter设置为fitCenter。

希望这些信息可以帮助您实现所需的目标。

Ionut Negru answered 2019-10-16T01:15:16Z

3 votes

另一种方法是创建常规图像的补丁9图像,并使其按您希望的方式缩放。

您可以通过在角落放置9个补缀点来使内容居中,从而明显保留比例(假定图像的最外边缘是可重复/透明的)。

希望你能明白。

xbakesx answered 2019-10-16T01:15:53Z

3 votes

我想在自定义Drawable类中做类似的事情。以下是重要的部分:

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);

}

}

}

}

通过这种方法,您可以灵活地应用所需的任何比例逻辑

Kamen Dobrev answered 2019-10-16T01:16:25Z

2 votes

使用a.ch描述的方法对我来说非常有用,除了我使用的这种比例类型对我需要的效果更好:

android:scaleType="centerCrop"

以下是可用的秤类型的完整列表:[http://developer.android.com/reference/android/widget/ImageView.ScaleType.html]

Benjamin Stark answered 2019-10-16T01:16:56Z

2 votes

尝试使用InsetDrawable(对我来说效果很好)。

只需为此提供您的可绘制对象,以及您希望从四个侧面中的任意一个插入的内容(或填充)。

InsetDrawable insetDrawable = new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom);

它专门用于设置背景可绘制,尺寸小于或小于视图的背景。

看这里 :[https://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html]

Ashutosh Chamoli answered 2019-10-16T01:17:41Z

1 votes

旧问题,但其他答案对我都没有作用。 但是,此xml代码执行了以下操作:

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentTop="true"

android:scaleType="centerCrop"

android:src="@drawable/background_image"/>

Incinerator answered 2019-10-16T01:18:05Z

1 votes

如果您的位图宽于高,则使用drawable-port。否则,请使用drawable-land。这与在ImageView上使用android:scaleType="centerCrop"的效果类似。

android:gravity="fill_vertical"

android:src="@drawable/image" />

如果支持多种方向,则可以在drawable-port文件夹中创建一个位图XML文件,在drawable-land文件夹中创建另一个位图XML文件。

Jason Robinson answered 2019-10-16T01:18:37Z

0 votes

为了使图像适合可用空间(或者如果您在dp中设置了宽度和高度),即使图像不太宽,我也尝试了以下方法。

在这里,我为方形图像设置了相同的宽度和高度[或者您都可以在adjust view bounds上同时设置]。

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="80dp"

android:layout_height="80dp"

android:layout_alignParentTop="true"

android:adjustViewBounds="true"

android:scaleType="fitCenter"

android:src="@drawable/background_image"/>

adjust view bounds和scale type center fit可以解决问题。

Abhinav Saxena answered 2019-10-16T01:19:16Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值