本文主要是介绍Android 画布canvas drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.d6import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.Viewclass CanvasView : View {private var mContext: Context? = nullprivate 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 = truemPaint.color = Color.REDmPaint.style = Paint.Style.STROKEmPaint.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()}}
效果:
这篇关于Android 画布canvas drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!