我想在我的线性布局的背景中添加一个图像,我知道属性将是 android: background="@drawable/login_bg"但现在我必须创建一个可绘制资源文件,在该文件中,我想要底部左侧和底部右侧为圆形,顶部左侧和右侧为矩形。 请记住:我需要一个带圆角背景的图像。
我试过这个link
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
</item>
<item android:drawable="@drawable/login_bg" />
</layer-list>
最佳答案
尝试使用这个自定义 View 。但是你必须把android:background改成android:src
class RadiusImageView: AppCompatImageView {
private val clipPath = Path()
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}
override fun onDraw(canvas: Canvas) {
//float radius = 36.0f;
val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
// 4 Pair of radius : top-left, top -right, bottom-right, bottom left, each pair is radius
// for rx and ry for each corner
clipPath.addRoundRect(rect, floatArrayOf(0f,0f,0f,0f,40f,40f,40f,40f) Path.Direction.CW)
canvas.clipPath(clipPath)
super.onDraw(canvas)
}
关于android - XML android 中带有圆底的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162010/