Android使用PorterDuffXfermode的模式PorterDuff.Mode.SRC_OUT实现橡皮擦,Kotlin(1)

发布于:2024-12-21 ⋅ 阅读:(17) ⋅ 点赞:(0)

Android使用PorterDuffXfermode的模式PorterDuff.Mode.SRC_OUT实现橡皮擦,Kotlin(1)

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatImageView


class MyView : AppCompatImageView {
    private var WIDTH = 0
    private var HEIGHT = 0

    private var mPaint: Paint = Paint()
    private var mBmpDst: Bitmap
    private var mBmpSrc: Bitmap
    private var mPath: Path
    private var mPreX = 0f
    private var mPreY = 0f
    private var mPorterDuffXfermode: PorterDuffXfermode

    constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {
        //WIDTH = resources.displayMetrics.widthPixels
        //HEIGHT = resources.displayMetrics.heightPixels

        //mPaint?.setColor(Color.RED)
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 30f

        mBmpSrc = BitmapFactory.decodeResource(resources, R.mipmap.pic, null)

        WIDTH = mBmpSrc.width
        HEIGHT = mBmpSrc.height

        //空Bitmap
        mBmpDst = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888)

        mPath = Path()

        /**
         *
         * SRC_OUT
         *
         * 当目标图像有图像时合成结果为空白像素;
         * 当目标图像没有图像时,合成结果显示源图像;
         * 如果把手指Path做为目标图像,在与源图像合成时,有手指轨迹的地方就变为空白像素,效果就是擦除。
         *
         */
        mPorterDuffXfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)
    }

    override fun onDraw(canvas: Canvas) {
        //super.onDraw(canvas)

        val layerId = canvas.saveLayer(0f, 0f, WIDTH.toFloat(), HEIGHT.toFloat(), null)

        //绘制Path
        val c = Canvas(mBmpDst)
        c.drawPath(mPath, mPaint)

        //绘制目标图像
        canvas.drawBitmap(mBmpDst, 0f, 0f, mPaint)

        //合成图像
        mPaint.setXfermode(mPorterDuffXfermode)
        canvas.drawBitmap(mBmpSrc, 0f, 0f, mPaint)

        mPaint.setXfermode(null)

        canvas.restoreToCount(layerId)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mPath.moveTo(event.x, event.y)
                mPreX = event.x
                mPreY = event.y

                return true
            }

            MotionEvent.ACTION_MOVE -> {
                val endX = (mPreX + event.x) / 2
                val endY = (mPreY + event.y) / 2
                mPath.quadTo(mPreX, mPreY, endX, endY)
                mPreX = event.x
                mPreY = event.y
            }

            MotionEvent.ACTION_UP -> {

            }
        }

        postInvalidate()

        return super.onTouchEvent(event)
    }
}

手指在屏幕上滑动,图像像被橡皮擦擦掉一样。

Android Canvas画布saveLayer与对应restoreToCount,Kotlin_android canvas.savelayer-CSDN博客文章浏览阅读982次,点赞8次,收藏8次。文章浏览阅读9.6k次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客。_android canvas.savelayerhttps://blog.csdn.net/zhangphil/article/details/135131896

Android Canvas图层saveLayer剪切clipRect原图对应Rect区域,Kotlin(1)_canvas savelayer cliprect-CSDN博客文章浏览阅读1k次,点赞21次,收藏22次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android Bitmap保存成至手机图片文件,Kotlin_android bitmap保存图片-CSDN博客。Android画布Canvas裁剪clipRect,Kotlin-CSDN博客。_canvas savelayer cliprecthttps://blog.csdn.net/zhangphil/article/details/135297013


网站公告

今日签到

点亮在社区的每一天
去签到