Android 画布canvas drawBitmap(Bitmap bitmap, float left, float top, Paint paint)

void    drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.

这个方法从(x,y)这个点绘制位图意思。没有返回值,参数有三个,分别是:

Bitmap bitmap:要绘制的位图

float left:绘制的位图左侧的位置

float top:绘制的位图顶部的位置

Paint paint:用于绘制位图的画布(可以为null)

如何使用:

需求:从(0,100)这个点去绘制位图。

关键代码:

 canvas?.drawBitmap(mBitmap,0f,100f, mPaint)

完整代码:

package com.lxm.apipro.canvas.d8

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()
        canvas?.drawBitmap(mBitmap,0f,100f, mPaint)
        canvas?.restore()

    }

}

效果图:

Android 画布canvas drawBitmap(Bitmap bitmap, float left, float top, Paint paint)_第1张图片

 

你可能感兴趣的:(每日学习一个api)