void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
Draw the bitmap using the specified matrix.
使用指定的矩阵绘制位图。也就是可以通过matrix的属性来控制如何绘制位图。该方法有三个参数,分别是:
Bitmap bitmap:要绘制的位图
Matrix matrix:绘制位图时用于变换位图的矩阵
Paint paint:画笔
如何使用:
需求:在坐标点为(100,100)开始绘制,然后图片顺时针旋转45°
关键代码:
matrix.postTranslate(100f, 100f)
matrix.preRotate(45f)
完整代码:
package com.lxm.apipro.canvas.d6
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
class CanvasView : View {
private var mContext: Context? = null
private var mBitmap: Bitmap =
BitmapFactory.decodeResource(resources, com.lxm.apipro.R.drawable.pic1)
private var mPaint: Paint = Paint()
constructor(context: Context?) : this(context, null)
constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
mContext = context
}
init {
mPaint.isAntiAlias = true
mPaint.color = Color.RED
mPaint.style = Paint.Style.STROKE
mPaint.strokeWidth = 5f
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.save()
var matrix = Matrix()
matrix.postTranslate(100f, 100f)
matrix.preRotate(45f)
canvas?.drawBitmap(mBitmap, matrix, null)
canvas?.restore()
}
}
效果: