Android BitmapShader简洁实现马赛克,Kotlin(二)

Android BitmapShader简洁实现马赛克,Kotlin(二)

这一篇

Android BitmapShader简洁实现马赛克,Kotlin(一)-CSDN博客

遗留一个问题,xml定义的MyView为wrap_content的宽高,如果改成其他模式如match_parent,因为background的Bitmap和draw时候的Bitmap不一致(background被拉伸了),导致手指划过屏幕涂抹的马赛克和实际的对不上,现在改进:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <com.myapp.MyView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>
Kotlin 复制代码
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.Shader
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.graphics.toRect


class MyView : AppCompatImageView {
    private var mPaint: Paint = Paint()
    private var mPath: Path = Path()

    private var mPreX = 0f
    private var mPreY = 0f
    private var mBitmapShader: BitmapShader? = null

    private val mResId = R.mipmap.npl

    private var mMosaicScaleFactor = 32f //值越大,马赛克效果越强。

    private var mSrcBmp: Bitmap? = null

    private var mSrcBmpW = 0
    private var mSrcBmpH = 0

    private var mScaleImageW = 0
    private var mScaleImageH = 0

    constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 100f

        mSrcBmp = BitmapFactory.decodeResource(resources, mResId, null)

        mSrcBmpW = mSrcBmp!!.width
        mSrcBmpH = mSrcBmp!!.height

        mScaleImageW = getScaleImageWidth()
        mScaleImageH = getScaleImageHeight()

        background = getBGBitmapDrawable()

        val mosaicBmp = getMosaicBmp(mSrcBmp!!)
        mBitmapShader = BitmapShader(mosaicBmp, Shader.TileMode.CLAMP, Shader.TileMode.REPEAT)

        mPaint.setShader(mBitmapShader)
    }

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

        canvas.drawPath(mPath, mPaint)
    }

    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)
    }

    private fun getSmallBmp(srcBmp: Bitmap): Bitmap {
        //空Bitmap
        val dstBmp =
            Bitmap.createBitmap((mSrcBmpW / mMosaicScaleFactor).toInt(), (mSrcBmpH / mMosaicScaleFactor).toInt(), Bitmap.Config.ARGB_8888)

        val c = Canvas(dstBmp)
        val mtx = Matrix()
        mtx.setScale(1 / mMosaicScaleFactor, 1 / mMosaicScaleFactor)
        c.drawBitmap(srcBmp, mtx, null)

        return dstBmp
    }

    private fun getMosaicBmp(srcBmp: Bitmap): Bitmap {
        val smallBmp = getSmallBmp(srcBmp)

        //空Bitmap
        val dstBmp = Bitmap.createBitmap(mScaleImageW, mScaleImageH, Bitmap.Config.ARGB_8888)

        val srcRectF = RectF(0f, 0f, smallBmp.width.toFloat(), smallBmp.height.toFloat())
        val dstRectF = RectF(0f, 0f, mScaleImageW.toFloat(), mScaleImageH.toFloat())

        val c = Canvas(dstBmp)
        c.drawBitmap(smallBmp, srcRectF.toRect(), dstRectF.toRect(), null)

        return dstBmp
    }

    private fun getBGBitmapDrawable(): BitmapDrawable {
        val bd = BitmapDrawable(resources, Bitmap.createScaledBitmap(mSrcBmp!!, mScaleImageW, mScaleImageH, true))
        return bd
    }

    private fun getScaleImageWidth(): Int {
        return resources.displayMetrics.widthPixels
    }

    private fun getScaleImageHeight(): Int {
        return (resources.displayMetrics.heightPixels * (mSrcBmpW / resources.displayMetrics.widthPixels.toFloat())).toInt()
    }
}

Android BitmapShader简洁实现马赛克,Kotlin(一)-CSDN博客文章浏览阅读465次,点赞7次,收藏5次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。https://blog.csdn.net/zhangphil/article/details/145308087https://blog.csdn.net/zhangphil/article/details/145308087

相关推荐
apihz21 分钟前
域名WHOIS信息查询免费API使用指南
android·开发语言·数据库·网络协议·tcp/ip
问道飞鱼38 分钟前
【移动端知识】移动端多 WebView 互访方案:Android、iOS 与鸿蒙实现
android·ios·harmonyos·多webview互访
aningxiaoxixi1 小时前
Android 之 audiotrack
android
枷锁—sha1 小时前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
Cao_Shixin攻城狮5 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦8 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl10 小时前
Mysql测试题
android·adb
游戏开发爱好者812 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号12 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h14 小时前
Android原生Dialog
android