AndroidキャンバスdrawBitmap(ビットマップビットマップ、Rect src、Rect dst、ペイントペイント)



Android Canvas Drawbitmap Bitmap Bitmap



void drawBitmap(ビットマップビットマップ、Rect src、Rect dst、ペイントペイント)
指定されたビットマップを描画し、目的の長方形を塗りつぶすように自動的に拡大縮小/平行移動します。

指定されたビットマップを描画し、自動的にズーム/パンしてターゲットの長方形を塗りつぶします。戻り値はありません。このメソッドには、次の3つのパラメーターがあります。



ビットマップビットマップ:描画されるビットマップ

Rect src:描画されるビットマップの領域。空にすることができます。空の場合、描画される領域はこのビットマップのサイズです。



Rect dst:自動的にズーム/変換されたビットマップがdst領域に表示されます。

使い方:

要件:画像の右下隅の4分の1の部分が、左上隅(300、300)と右下隅(600、600)の長方形の内側に表示されます。



キーコード:

var src = Rect(mBitmap.width / 2,mBitmap.height / 2,mBitmap.width,mBitmap.height) var dst = Rect(300,300,600,600)

完全なコード:

package com.lxm.apipro.canvas.d7 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 src = Rect(mBitmap.width / 2,mBitmap.height / 2,mBitmap.width,mBitmap.height) var dst = Rect(300,300,600,600) canvas?.drawBitmap(mBitmap, src,dst, mPaint) canvas?.restore() } }

効果画像: