【问题标题】:Custom border layout contains half of a circle自定义边框布局包含半个圆
【发布时间】:2020-11-04 10:58:47
【问题描述】:

我需要创建带有两个布局标题和描述的 xml。 在标题布局中,我需要添加边框在左下角和右下角有一半的圆圈,并且描述布局边框在左上角和右上方有一半的圆圈。 这是我的设计

我可以在矩形半径边界线上创建两个半圆,但我不想使用它。 我怎么能用另一种解决方案来做到这一点? 请给我关键工作或解决方案! 非常感谢!

【问题讨论】:

    标签: android xml android-shapedrawable


    【解决方案1】:

    您可以使用ShapeAppearanceModel 定义自定义CornerTreatment 以应用于组件。 比如:

        val radius = resources.getDimension(R.dimen.default_corner_radius)
        val title_layout = findViewById<LinearLayout>(R.id.title_layout)
        
        val titleShapeModel = ShapeAppearanceModel().toBuilder()
                .setTopLeftCorner(CornerFamily.ROUNDED, radius)
                .setTopRightCorner(CornerFamily.ROUNDED, radius)
                .setBottomLeftCorner(ConcaveRoundedCornerTreatment()).setBottomLeftCornerSize(radius)
                .setBottomRightCorner(ConcaveRoundedCornerTreatment()).setBottomRightCornerSize(radius)
                .build()
        val titleBackground = MaterialShapeDrawable(titleShapeModel)
        titleBackground.setStroke(1f, ContextCompat.getColor(this, R.color.colorPrimaryDark))
    
        ViewCompat.setBackground(title_layout, titleBackground)
    

    ConcaveRoundedCornerTreatment 在哪里:

    class ConcaveRoundedCornerTreatment : CornerTreatment() {
    
        override fun getCornerPath(
                shapePath: ShapePath,
                angle: Float,
                interpolation: Float,
                radius: Float
        ) {
            val interpolatedRadius = radius * interpolation
            shapePath.reset(0f, interpolatedRadius, ANGLE_LEFT, ANGLE_LEFT - angle)
            shapePath.addArc(
                    -interpolatedRadius,
                    -interpolatedRadius,
                    interpolatedRadius,
                    interpolatedRadius,
                    ANGLE_BOTTOM,
                    -angle
            )
        }
    
        companion object {
            const val ANGLE_LEFT = 180f
            const val ANGLE_BOTTOM = 90f
        }
    }
    

    对描述布局做同样的事情:

    如果您使用像 CardView 这样具有内置 shapeAppearanceModel 的视图:

    cardView.shapeAppearanceModel = cardView.shapeAppearanceModel.toBuilder()
            .setTopRightCorner(concaveRoundedCornerTreatment).
            .........
            .build()
    

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 1970-01-01
      • 2022-01-21
      • 2017-04-05
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多