Canvas->左上角坐标系和中心坐标系的转换

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <com.yang.app.MyView
        android:id="@+id/iv"
        android:layout_width="300dp"
        android:layout_height="300dp"/>
</LinearLayout>

自定义View代码

class MyView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private val spanValue = 200f
    private val mNewPaint = Paint().apply {
        isAntiAlias = true
        isFilterBitmap = true
        style = Paint.Style.STROKE   // 只绘制边
        strokeWidth = 10f             // 设置线宽
        color = Color.RED
    }
    private val mOldPaint = Paint().apply {
        isAntiAlias = true
        isFilterBitmap = true
        style = Paint.Style.STROKE   // 只绘制边
        strokeWidth = 10f             // 设置线宽
        color = Color.BLUE
    }

    private val mFinalPaint = Paint().apply {
        isAntiAlias = true
        isFilterBitmap = true
        style = Paint.Style.STROKE   // 只绘制边
        strokeWidth = 10f             // 设置线宽
        color = Color.GREEN
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 1. 左上角(0, 0)开始画出整个View的轮廓
        canvas?.drawRect(0f, 0f, width.toFloat(), height.toFloat(), mNewPaint)
        // 2. 左上角(0, 0)开始画一个矩形
        canvas?.drawRect(0f, 0f, spanValue, spanValue, mOldPaint)

        // 3. 平移到当前View中心点(width/2, height/2)画一个矩形
        val matrix = Matrix()
        matrix.setTranslate((width / 2).toFloat(), (height / 2).toFloat())
        canvas?.concat(matrix)
        canvas?.drawCircle(0f, 0f, 20f, Paint().apply {
            color = Color.BLACK
        })
        canvas?.drawRect(-spanValue/2, -spanValue/2, spanValue/2, spanValue/2, mNewPaint)

        // 4. 返回View坐标系左上角(0, 0)再绘制一个矩形
        matrix?.invert(matrix)
        canvas?.concat(matrix)
        canvas?.drawRect(0f, 0f, spanValue * 2, spanValue * 2, mFinalPaint)
    }
}

效果图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Canvas坐标转换为屏幕坐标主要涉及到两个概念:Canvas坐标系和屏幕的坐标系Canvas坐标系是指Canvas画布上的坐标系,它的原点在Canvas左上角,x轴向右增加,y轴向下增加;屏幕坐标系是指计算机屏幕上的坐标系,它的原点在屏幕左上角,x轴向右增加,y轴向下增加。 在确定Canvas坐标和屏幕坐标之间的转换关系之前,我们首先需要获取Canvas的位置信息,也就是左上角点在屏幕坐标系上的x和y坐标。可以使用以下代码获取: ```javascript const canvas = document.getElementById('myCanvas'); const canvasX = canvas.getBoundingClientRect().left; const canvasY = canvas.getBoundingClientRect().top; ``` 接下来我们需要考虑Canvas上的元素位置信息,如矩形、圆形、文本等。Canvas坐标转换为屏幕坐标的方法有两种: 1. 使用CanvasRenderingContext2D的`transform()`方法:可以通过该方法将Canvas坐标系转换为屏幕坐标系,从而直接获取元素在屏幕上的位置信息。 ```javascript const ctx = canvas.getContext('2d'); ctx.fillRect(50, 50, 100, 100); //画一个矩形 //获取元素在屏幕上的位置信息 const screenX = 50 + canvasX; const screenY = 50 + canvasY; const screenWidth = 100; const screenHeight = 100; ``` 2. 手动计算转换:将Canvas坐标系中的坐标转换为屏幕坐标系中的坐标,需要注意的是,由于两个坐标系的原点和方向不同,因此转换时需要进行坐标轴翻转和坐标系平移等操作。 ```javascript const ctx = canvas.getContext('2d'); ctx.fillRect(50, 50, 100, 100); //画一个矩形 //手动计算元素在屏幕上的位置信息 const x = 50; const y = 50; const width = 100; const height = 100; const screenX = x + canvasX; const screenY = canvas.height - y - height + canvasY; const screenWidth = width; const screenHeight = height; ``` 在实际应用中,我们通常会采用第一种方式进行转换,因为它更为简洁方便,同时也是推荐使用的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值