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