本文主要是介绍Android 画布canvas clipOutPath(Path path),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
boolean clipOutPath(Path path)
Set the clip to the difference of the current clip and the specified path.
Canvas 中clipOutPath(Path path)方法,是对画布按照path进行裁剪的意思。参数path是由直线段,二次曲线和三次曲线组成的复合(多轮廓)几何路径。该方法的返回值是布尔类型,裁剪结果不为空返回true。
如何使用:
把下面一直图片,左下角和右下角处理成圆角,左上角和右上角不用处理。
关键代码就是
mPath.addRoundRect(RectF(left,top,right, bottom), rids, Path.Direction.CW)if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {isClipPath = canvas?.clipOutPath(mPath)}else{isClipPath = canvas?.clipPath(mPath)}canvas?.drawBitmap(mBitmap,left,top,null)
完整代码:
package com.lxm.apipro.canvasimport android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.Viewclass CanvasView : View {private var mContext: Context? = nullprivate val mPath: Path = Path()private val mPath2: Path = Path()private var mBitmap: Bitmap = BitmapFactory.decodeResource(resources, com.lxm.apipro.R.drawable.pic1)private val rids = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f, 20.0f, 20.0f, 20.0f, 20.0f)private var mPaint: Paint? = nullconstructor(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 = contextmPaint = Paint()mPaint?.isAntiAlias = true}override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)canvas?.save()mPath.reset()var picWith = mBitmap.widthvar picHeight = mBitmap.heightvar top = 300f - picHeight / 2fvar left = 300f - picWith / 2fvar right = left + picWithvar bottom = top + picHeightmPath.addRoundRect(RectF(left,top,right, bottom), rids, Path.Direction.CW)// Path.Direction.CW 顺时针var isClipPath: Boolean?if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {isClipPath = canvas?.clipOutPath(mPath)}else{isClipPath = canvas?.clipPath(mPath)}Log.d("xxx","isClipPath:$isClipPath")canvas?.drawBitmap(mBitmap,left,top,null)canvas?.restore()}}
运行效果:
这篇关于Android 画布canvas clipOutPath(Path path)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!