Matrix 使用

这篇具有很好参考价值的文章主要介绍了Matrix 使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Matrix 使用

val maxWidth = 100f
            val maxHeight = 100f

            val maxY = 13f
            val minY = 2f

            val matrix = Matrix()
            matrix.reset()
            //val tY = calculate(minY,maxY)
            //matrix.postTranslate(0f,-(maxY - minY))
//            matrix.postTranslate(0f,-(maxY - minY))
//            matrix.postTranslate(0f,-(minY))
            matrix.postTranslate(0f,-(maxY))

            val max = maxY - minY
            val sy = maxHeight/max
            Log.e("vic","sy = $sy  ${sy *(maxY - minY)}")
            //val sy = calculate(minY,maxY)
            val sx = maxWidth/4

            matrix.postScale(sx,-sy)
            val array = floatArrayOf(0f,maxY,1f,10f,2f,7f,3f,2f,4f,minY)
            printArray(array)
            matrix.mapPoints(array)
            printArray(array)
private fun printArray(array: FloatArray) {
        val list = ArrayList<Float>()
        list.addAll(array.toList())
        Log.e("vic","$list")
    }

矩阵的对坐标的相互转换文章来源地址https://www.toymoban.com/news/detail-786531.html

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


    val circlePaint:Paint by lazy {
         Paint().apply {
            color = Color.parseColor("#2E4A9D")
            strokeWidth = 5f
            style = Paint.Style.FILL
            isAntiAlias = true
        }
//        val circle2Paint = Paint()
//        circle2Paint.color = Color.parseColor("#2E4A9D")
//        circle2Paint.strokeWidth = px2dp(circleR1.toFloat()).toFloat()
//        circle2Paint.isAntiAlias = true
//        circle2Paint.style = Paint.Style.STROKE
//        circle2Paint
    }

    var circleR1 = 2
    var circleR2 = 4


    private val circle2Paint: Paint by lazy {
        val circle2Paint = Paint()
        circle2Paint.color = Color.parseColor("#2E4A9D")
        circle2Paint.strokeWidth = px2dp(circleR1.toFloat()).toFloat()
        circle2Paint.isAntiAlias = true
        circle2Paint.style = Paint.Style.STROKE
        circle2Paint
    }

    override fun onDrawForeground(canvas: Canvas?) {
        super.onDrawForeground(canvas)
    }

    override fun onDraw(canvas: Canvas) {

        canvas.drawColor(Color.RED)

        val maxWidth:Float = width.toFloat()//-300f
        val maxHeight:Float = height.toFloat()//-300f


        val maxY = 10f
        val minY = 0f



        val matrix = Matrix()

        val matrixX = Matrix()





//        matrix.postTranslate(0f,-(maxY))
//
//        val max = maxY - minY
//        val sy = maxHeight/max
//        val sx = maxWidth/4
//
//        matrix.postScale(sx,-sy)




        val sx = maxWidth/4

        val top = 120f

        canvas.drawLine(0f,top,width.toFloat(),top,circlePaint)


        val yMin = minY
        val yMax = maxY
        val bottom = maxHeight-150f

        canvas.drawLine(0f,bottom,width.toFloat(),bottom,circlePaint)


        // 先贴顶
        matrix.postTranslate(0f, top - yMin)
        val sy = (bottom - top) / (yMax - yMin)
        // 再缩放
        matrix.postScale(1f, sy, 0f, top)

        matrixX.postScale(sx,1f)



        val array = floatArrayOf(0f,maxY,1f,10f,2f,7f,3f,2f,4f,minY)
        printArray(array)
        val tempMatrix = Matrix()
        tempMatrix.postConcat(matrix)
        tempMatrix.postConcat(matrixX)
        tempMatrix.mapPoints(array)
        printArray(array)

        for (index in 0 until array.size step 2){
            //canvas.drawLine(array[index],array[index+1],array[index]+100f,array[index+1]+100f,circlePaint)
            //canvas.drawCircle(array[index]+150f,array[index+1]+150f,20f,circlePaint)

            canvas.drawCircle(array[index],array[index+1],20f,circlePaint)


        }

        val invertMatrix = Matrix()
        tempMatrix.invert(invertMatrix)

        val _touchX = mTouchX
        val _touchY = mTouchY-120f+150f
        val array2 = floatArrayOf(_touchX, _touchY)
        invertMatrix.mapPoints(array2)
        printArray(array2)


        val temp2Matrix = Matrix()
        temp2Matrix.postConcat(matrix)
        temp2Matrix.postConcat(matrixX)
        temp2Matrix.mapPoints(array2)

        printArray(array2)

        canvas.drawCircle(array2[0],array2[1],30f,circlePaint)


        //canvas.drawBitmap(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_foreground),0f,0f,circlePaint)




    }
    

    var mTouchX = 0f
    var mTouchY = 0f

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        mTouchX = event?.x?:0f
        mTouchY = event?.y?:0f
        invalidate()
        return true
    }

    private fun px2dp(px: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, context.resources.displayMetrics).toInt()
    }

    private fun printArray(array: FloatArray) {
        val list = ArrayList<Float>()
        list.addAll(array.toList())
        Log.e("vic","$list")
    }


    
}

到了这里,关于Matrix 使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包