android - XML android 中带有圆底的背景图像

标签 android android-layout android-xml

Desired Implementation

我想在我的线性布局的背景中添加一个图像,我知道属性将是 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/

相关文章:

android - 他们如何做到这一点?动画抽屉旋转可能吗?

android - ConstraintLayout 匹配兄弟 View 高度,以较高者为准

android - 填充在某些设备上不起作用

android - 如何为 Android Honeycomb 应用程序创建每周日历 View ?

java - ImageView centerCrop 溢出 ImageView 边界?

android - 如何在libPd中播放WAV文件?

android - ActionBar - 具有居中 ImageView 的自定义 View ,两侧的操作项

android - 从 Android 中的 URL 方案打开我的应用程序

java - 如何在LayerList android的中心添加图像

android - 具有不同角半径的 CardView