Android RenderEffect对Bitmap高斯模糊(毛玻璃),Kotlin(1)
Android RenderEffect对Bitmap高斯模糊(毛玻璃),Kotlin(1)
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.HardwareRenderer
import android.graphics.PixelFormat
import android.graphics.RenderEffect
import android.graphics.RenderNode
import android.graphics.Shader
import android.hardware.HardwareBuffer
import android.media.ImageReader
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivityclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.image_layout)//val imageView1 = findViewById<ImageView>(R.id.image1)val imageView2 = findViewById<ImageView>(R.id.image2)val srcBitmap = BitmapFactory.decodeResource(resources, R.mipmap.img)val radius = 10fval t = System.currentTimeMillis()val imageReader = ImageReader.newInstance(srcBitmap.width,srcBitmap.height,PixelFormat.RGBA_8888,1,HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT)val renderNode = RenderNode("BlurEffect")val hardwareRenderer = HardwareRenderer()hardwareRenderer.setSurface(imageReader.surface)hardwareRenderer.setContentRoot(renderNode)renderNode.setPosition(0, 0, imageReader.width, imageReader.height)val blurRenderEffect = RenderEffect.createBlurEffect(radius, radius,Shader.TileMode.MIRROR)renderNode.setRenderEffect(blurRenderEffect)val renderCanvas = renderNode.beginRecording()renderCanvas.drawBitmap(srcBitmap, 0f, 0f, null)renderNode.endRecording()hardwareRenderer.createRenderRequest().setWaitForPresent(true).syncAndDraw()val image = imageReader.acquireNextImage() ?: throw RuntimeException("No Image")val hardwareBuffer = image.hardwareBuffer ?: throw RuntimeException("No HardwareBuffer")val bmp = Bitmap.wrapHardwareBuffer(hardwareBuffer, null)?: throw RuntimeException("Create Bitmap Failed")Log.d("fly", "cost time=${System.currentTimeMillis() - t}")imageView2.setImageBitmap(bmp)//清理工作hardwareBuffer.close()image.close()imageReader.close()renderNode.discardDisplayList()hardwareRenderer.destroy()}
}
遗留问题,需要抽象封装一下,复用部分关键类对象。
Android新版高斯模糊(毛玻璃)官方实现,Kotlin-CSDN博客文章浏览阅读577次,点赞13次,收藏4次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android BitmapShader简洁实现马赛克/高斯模糊(毛玻璃),Kotlin(三)_android kotlin实现毛玻璃遮罩-CSDN博客。https://blog.csdn.net/zhangphil/article/details/145575404