自学内容网 自学内容网

Canvas -> 等比放大/缩小绘制Rect

XML文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.myapplication.MyView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

自定义View代码

class MyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        alpha = 128
        strokeWidth = 10f
    }

    private val originalRect = RectF(200f, 200f, 800f, 1600f)

    private val rect1: RectF
    private val rect2: RectF
    private val rect3: RectF

    init {
        rect1 = originalRect
        rect2 = adjustDashRect(originalRect, 100f)
        rect3 = enlargeDashRect(originalRect, 100f)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        paint.color = Color.RED
        canvas.drawRect(rect1, paint)

        paint.color = Color.GREEN
        canvas.drawRect(rect2, paint)

        paint.color = Color.BLUE
        canvas.drawRect(rect3, paint)
    }

    private fun adjustDashRect(rect: RectF, paddingPx: Float): RectF {
        val rectCopy = RectF(rect)
        val width = rectCopy.width()
        val height = rectCopy.height()

        val aspectRatio = width / height

        val newWidth: Float
        val newHeight: Float

        if (width > height) {
            newWidth = width - 2 * paddingPx
            newHeight = newWidth / aspectRatio
        } else {
            newHeight = height - 2 * paddingPx
            newWidth = newHeight * aspectRatio
        }

        val horizontalPadding = (width - newWidth) / 2
        val verticalPadding = (height - newHeight) / 2

        rectCopy.left += horizontalPadding
        rectCopy.top += verticalPadding
        rectCopy.right -= horizontalPadding
        rectCopy.bottom -= verticalPadding
        return rectCopy
    }

    private fun enlargeDashRect(rect: RectF, paddingPx: Float): RectF {
        val rectCopy = RectF(rect)
        val width = rectCopy.width()
        val height = rectCopy.height()

        val aspectRatio = width / height

        val newWidth: Float
        val newHeight: Float

        if (width > height) {
            newWidth = width + 2 * paddingPx
            newHeight = newWidth / aspectRatio
        } else {
            newHeight = height + 2 * paddingPx
            newWidth = newHeight * aspectRatio
        }

        val horizontalPadding = (newWidth - width) / 2
        val verticalPadding = (newHeight - height) / 2

        rectCopy.left -= horizontalPadding
        rectCopy.top -= verticalPadding
        rectCopy.right += horizontalPadding
        rectCopy.bottom += verticalPadding
        return rectCopy
    }
}

效果图

在这里插入图片描述


原文地址:https://blog.csdn.net/sunshine_guo/article/details/145287962

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!