Android BitmapShader简洁实现马赛克,Kotlin(一)
这一篇,
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现马赛克效果,Kotlin(3)-CSDN博客
基于PorterDuffXfermode实现马赛克,理解上有一定难度。
这次,基于BitmapShader简洁实现马赛克。
<?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.MyViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout>
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.Shader
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatImageViewclass MyView : AppCompatImageView {private var mPaint: Paint = Paint()private var mPath: Path = Path()private var mPreX = 0fprivate var mPreY = 0fprivate var mBitmapShader: BitmapShader? = nullprivate val mResId = R.mipmap.nplprivate var mMosaicScaleFactor = 16f //值越大,马赛克效果越强。private var mSrcBmp: Bitmap? = nullprivate var mSrcBmpW = 0private var mSrcBmpH = 0constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {mPaint.style = Paint.Style.STROKEmPaint.strokeWidth = 35fmSrcBmp = BitmapFactory.decodeResource(resources, mResId, null)background = BitmapDrawable(resources, mSrcBmp)mSrcBmpW = mSrcBmp!!.widthmSrcBmpH = mSrcBmp!!.heightval 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.xmPreY = event.yreturn true}MotionEvent.ACTION_MOVE -> {val endX = (mPreX + event.x) / 2val endY = (mPreY + event.y) / 2mPath.quadTo(mPreX, mPreY, endX, endY)mPreX = event.xmPreY = event.y}MotionEvent.ACTION_UP -> {}}postInvalidate()return super.onTouchEvent(event)}private fun getSmallBmp(srcBmp: Bitmap): Bitmap {//空Bitmapval 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)//空Bitmapval dstBmp = Bitmap.createBitmap(mSrcBmpW, mSrcBmpH, Bitmap.Config.ARGB_8888)val mtx = Matrix()mtx.setScale(mMosaicScaleFactor, mMosaicScaleFactor)val c = Canvas(dstBmp)c.drawBitmap(smallBmp, mtx, null)return dstBmp}
}
当手指在图上划过时,划过的轨迹马赛克:
Android BitmapShader更简易的实现刮刮乐功能,Kotlin_kotlin shaderbrush-CSDN博客文章浏览阅读816次,点赞8次,收藏19次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android Bitmap保存成至手机图片文件,Kotlin_android bitmap保存图片-CSDN博客。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。_kotlin shaderbrushhttps://blog.csdn.net/zhangphil/article/details/145143789https://blog.csdn.net/zhangphil/article/details/145143789https://blog.csdn.net/zhangphil/article/details/145143789
Android简洁缩放Matrix实现图像马赛克,Kotlin_kotlin matrix-CSDN博客文章浏览阅读916次,点赞5次,收藏12次。以图形图像界经典的实验例图Lenna为例,当手指在图片上滑过后,形成马赛克的: 写一个MosaicView继承自AppCompatImageView:package com.zhangphil;原理,通过Matrix把一个原图缩小到原先的1/n,然后再把缩小后的小图放大n倍,自然就是马赛克效果(相当于是放大后像素“糊”成一片了)。Android图形图像处理:马赛克(Mosaic)效果_android对图片部分区域做马赛克-CSDN博客。_kotlin matrixhttps://blog.csdn.net/zhangphil/article/details/144559418https://blog.csdn.net/zhangphil/article/details/144559418https://blog.csdn.net/zhangphil/article/details/144559418