Android画布Canvas裁剪clipRect,Kotlin

傷城~ 2024-02-05 18:05 120阅读 0赞

Android画布Canvas裁剪clipRect,Kotlin

#

  1. private fun mydraw() {
  2. val originBmp = BitmapFactory.decodeResource(resources, R.mipmap.pic).copy(Bitmap.Config.ARGB_8888, true)
  3. val newBmp = Bitmap.createBitmap(originBmp.width, originBmp.height, Bitmap.Config.ARGB_8888)
  4. val canvas = Canvas(newBmp)
  5. //把原图绘制在画布Canvas
  6. canvas.drawBitmap(originBmp, 0f, 0f, null)
  7. val paint = Paint(Paint.ANTI_ALIAS_FLAG)
  8. paint.color = Color.BLUE
  9. paint.style = Paint.Style.STROKE
  10. paint.strokeWidth = 30f
  11. val centerX = originBmp.width / 2
  12. val centerY = originBmp.height / 2
  13. val w = 300
  14. val h = 200
  15. val rect = Rect(centerX - w / 2, centerY - h / 2, centerX + w / 2, centerY + h / 2)
  16. canvas.clipRect(rect) //选(裁剪)出一块中心区域。
  17. iv1?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
  18. canvas.drawColor(Color.RED) //在这块中心区域绘制颜色。
  19. iv2?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
  20. canvas.drawRect(rect, paint) //在这块中心区域边框绘制线。
  21. iv3?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
  22. }
  23. private fun saveBitmapToFile(bm: Bitmap): File? {
  24. var saveFile: File? = null
  25. val savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
  26. if (!Files.exists(Paths.get(savePath))) {
  27. Log.d("保存文件", "${savePath}不存在!")
  28. } else {
  29. saveFile = File(savePath, System.currentTimeMillis().toString() + ".jpeg")
  30. try {
  31. val saveImgOut = FileOutputStream(saveFile)
  32. //压缩
  33. bm.compress(Bitmap.CompressFormat.JPEG, 90, saveImgOut)
  34. saveImgOut.flush()
  35. saveImgOut.close()
  36. Log.d("保存文件", "Bitmap保存至 ${saveFile.absoluteFile.toPath()}")
  37. } catch (e: Exception) {
  38. e.printStackTrace()
  39. }
  40. }
  41. return saveFile
  42. }

9ccabbf927fd4ff1b3e53cf087858d15.png

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin-CSDN博客文章浏览阅读1.3k次,点赞19次,收藏15次。文章浏览阅读9.6k次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客。favicon32.icohttps://blog.csdn.net/zhangphil/article/details/134818221

Android Bitmap保存成至手机图片文件,Kotlin_android bitmap保存图片-CSDN博客文章浏览阅读883次,点赞8次,收藏5次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。假设根目录的Pictures下已经有两张图片zhang.jpg和phil.jpg,最终把这两张图片合并成zhangphil.jpg的长图:package zhangphil.test;_android bitmap保存图片favicon32.icohttps://blog.csdn.net/zhangphil/article/details/134603333

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android Canvas画布

    在本教程中,我们将讨论 Android 的一个非常重要的部分,即 Canvas。这是每个开发人员都喜欢远离的领域。本教程的目的是让您更加了解和轻松使用 Android Canv

    相关 canvas画布

    canvas标签 <canvas></canvas> 接下来使用支持H5的浏览器打开该页面,将会看到如下内容: ![在这里插入图片描述][watermark

    相关 canvas-画布

    概述 HTML5 的 canvas 元素用于定义画布,是一个矩形区域,默认颜色为白色,默认大小为 300 \ 150;通过 JavaScript 可以在该区域上绘制图像