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")
}
}
文章来源:https://www.toymoban.com/news/detail-786531.html
到了这里,关于Matrix 使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!