Android画布Canvas裁剪clipRect,Kotlin
2023-12-24 23:31:54
Android画布Canvas裁剪clipRect,Kotlin
?
?
private fun mydraw() {
val originBmp = BitmapFactory.decodeResource(resources, R.mipmap.pic).copy(Bitmap.Config.ARGB_8888, true)
val newBmp = Bitmap.createBitmap(originBmp.width, originBmp.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(newBmp)
//把原图绘制在画布Canvas
canvas.drawBitmap(originBmp, 0f, 0f, null)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.BLUE
paint.style = Paint.Style.STROKE
paint.strokeWidth = 30f
val centerX = originBmp.width / 2
val centerY = originBmp.height / 2
val w = 300
val h = 200
val rect = Rect(centerX - w / 2, centerY - h / 2, centerX + w / 2, centerY + h / 2)
canvas.clipRect(rect) //选(裁剪)出一块中心区域。
iv1?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
canvas.drawColor(Color.RED) //在这块中心区域绘制颜色。
iv2?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
canvas.drawRect(rect, paint) //在这块中心区域边框绘制线。
iv3?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
}
private fun saveBitmapToFile(bm: Bitmap): File? {
var saveFile: File? = null
val savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
if (!Files.exists(Paths.get(savePath))) {
Log.d("保存文件", "${savePath}不存在!")
} else {
saveFile = File(savePath, System.currentTimeMillis().toString() + ".jpeg")
try {
val saveImgOut = FileOutputStream(saveFile)
//压缩
bm.compress(Bitmap.CompressFormat.JPEG, 90, saveImgOut)
saveImgOut.flush()
saveImgOut.close()
Log.d("保存文件", "Bitmap保存至 ${saveFile.absoluteFile.toPath()}")
} catch (e: Exception) {
e.printStackTrace()
}
}
return saveFile
}
?
?
?
?
?
?
文章来源:https://blog.csdn.net/zhangphil/article/details/135156671
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!