Android裁剪背景

Android裁剪背景,android,background,drawable,crop,Android,Background,Drawable,Crop,我有一个LinearLayout,宽度设置为fill\u parent,高度设置为wrap\u content。现在我想给它一个png文件的背景,但是用传统的方法会导致线性布局调整大小,以显示整个背景,而不是裁剪额外的部分 如何设置LinearLayout的背景,使其不会调整大小 谢谢似乎只有使用ImageView并相应地设置scaleType参数,才能实现这一点。快速解决方法是使用FrameLayout并将ImageView放在另一个具有透明背景的布局下 代码: 背景位图的默认行为是完全填充容

我有一个
LinearLayout
,宽度设置为
fill\u parent
,高度设置为
wrap\u content
。现在我想给它一个png文件的背景,但是用传统的方法会导致
线性布局
调整大小,以显示整个背景,而不是裁剪额外的部分

如何设置
LinearLayout
的背景,使其不会调整大小


谢谢

似乎只有使用
ImageView
并相应地设置
scaleType
参数,才能实现这一点。快速解决方法是使用
FrameLayout
并将
ImageView
放在另一个具有透明背景的布局下

代码:


背景位图的默认行为是完全填充容器,并根据需要增加容器的水平和垂直大小。解决方案是创建可绘制位图资源并更改重力

检查以下链接并查找可能的重力值。然后,您的视图应该只引用可绘制的资源,您应该不会有问题


将此代码放在xml活动中,例如activity_main.xml

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/image" />


我通过子类化
LinearLayout
并重写
onMeasure(int,int)
回调,然后更新布局XML以使用我的新布局类型,解决了这个问题。我无权发布准确的代码,但我所做的是调用
super.onMeasure(int,int)
实现,并检查测量的高度是否返回为我背景图像的准确高度。如果是这样,我获取所有子视图的测量高度,然后计算新高度值并将其传递给
setMeasuredDimension(int,int)

传统方法是什么?你能把它的样子和它应该看起来怎么样的图片放进去吗?这并没有提供足够的细节如何做透明背景?每件物品都必须有透明的背景色吗?
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/image" />