Android画布Canvas绘图scale,Kotlin

我会带着你远行 2024-02-05 12:40 142阅读 0赞

Android画布Canvas绘图scale,Kotlin

#

46334432917a469da58fb423af0aade4.png

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@android:color/darker_gray"
  7. android:orientation="vertical"
  8. app:divider="@android:drawable/divider_horizontal_bright"
  9. app:dividerPadding="5dp"
  10. app:showDividers="beginning|middle|end">
  11. <ImageView
  12. android:id="@+id/iv"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center_horizontal"
  16. android:layout_margin="10dp"
  17. android:background="@drawable/ic_launcher_background"
  18. android:scaleType="fitCenter"
  19. android:src="@mipmap/pic" />
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal">
  24. <ImageView
  25. android:id="@+id/iv1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_margin="10dp"
  29. android:background="@drawable/ic_launcher_background" />
  30. <ImageView
  31. android:id="@+id/iv2"
  32. android:layout_width="300px"
  33. android:layout_height="wrap_content"
  34. android:layout_margin="10dp"
  35. android:background="@drawable/ic_launcher_background" />
  36. <ImageView
  37. android:id="@+id/iv3"
  38. android:layout_width="300px"
  39. android:layout_height="wrap_content"
  40. android:layout_margin="10dp"
  41. android:background="@drawable/ic_launcher_background" />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:orientation="horizontal">
  47. <ImageView
  48. android:id="@+id/iv4"
  49. android:layout_width="300px"
  50. android:layout_height="wrap_content"
  51. android:layout_margin="10dp"
  52. android:background="@drawable/ic_launcher_background" />
  53. <ImageView
  54. android:id="@+id/iv5"
  55. android:layout_width="300px"
  56. android:layout_height="wrap_content"
  57. android:layout_margin="10dp"
  58. android:background="@drawable/ic_launcher_background" />
  59. <ImageView
  60. android:id="@+id/iv6"
  61. android:layout_width="300px"
  62. android:layout_height="wrap_content"
  63. android:layout_margin="10dp"
  64. android:background="@drawable/ic_launcher_background" />
  65. </LinearLayout>
  66. </androidx.appcompat.widget.LinearLayoutCompat>
  67. import android.graphics.Bitmap
  68. import android.graphics.Canvas
  69. import android.graphics.Color
  70. import android.graphics.Paint
  71. import android.graphics.RectF
  72. import android.graphics.drawable.BitmapDrawable
  73. import android.os.Bundle
  74. import android.util.Log
  75. import android.widget.ImageView
  76. import androidx.appcompat.app.AppCompatActivity
  77. import androidx.lifecycle.lifecycleScope
  78. import kotlinx.coroutines.Dispatchers
  79. import kotlinx.coroutines.delay
  80. import kotlinx.coroutines.launch
  81. class MainActivity : AppCompatActivity() {
  82. private var iv: ImageView? = null
  83. private var iv1: ImageView? = null
  84. override fun onCreate(savedInstanceState: Bundle?) {
  85. super.onCreate(savedInstanceState)
  86. setContentView(R.layout.activity_main)
  87. iv = findViewById(R.id.iv)
  88. iv1 = findViewById(R.id.iv1)
  89. lifecycleScope.launch(Dispatchers.Main) {
  90. delay(500)
  91. f1()
  92. }
  93. }
  94. private fun f1() {
  95. val bitmap = ((iv?.drawable as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
  96. //val bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888)
  97. val canvas = Canvas(bitmap)
  98. //canvas.drawColor(Color.LTGRAY) //铺满
  99. val w = bitmap.width
  100. val h = bitmap.height
  101. Log.d("fly", "w=$w h=$h")
  102. val paint = Paint()
  103. paint.isAntiAlias = true
  104. paint.color = Color.RED
  105. paint.style = Paint.Style.FILL
  106. val left = 60f
  107. val top = 60f
  108. val rectF = RectF(left, top, left + w / 2, top + h / 2)
  109. canvas.drawRect(rectF, paint)
  110. canvas.scale(0.5f, 0.5f) //缩小。
  111. paint.color = Color.YELLOW
  112. canvas.drawRect(rectF, paint)
  113. canvas.scale(0.3f, 0.3f) //缩小。
  114. paint.color = Color.BLUE
  115. canvas.drawRect(rectF, paint)
  116. iv1?.setImageBitmap(bitmap)
  117. }
  118. }

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin-CSDN博客文章浏览阅读471次,点赞9次,收藏8次。文章浏览阅读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

发表评论

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

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

相关阅读

    相关 Android Canvas画布

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

    相关 canvas画布

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

    相关 canvas-画布

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