Advertisement
rizkirchm

ImageVIew with TouchLister

Nov 7th, 2022 (edited)
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.88 KB | Source Code | 0 0
  1. custSign.setOnTouchListener { v, event ->
  2.             xCust = event.x
  3.             yCust = event.y
  4.  
  5.             when (event.action) {
  6.                 MotionEvent.ACTION_DOWN -> {
  7. //                    val layoutParams = FrameLayout.LayoutParams(
  8. //                        v.layoutParams.width,
  9. //                        v.layoutParams.height
  10. //                    )
  11. //                    layoutParams.setMargins(
  12. //                        (event.rawX - v.width / 2).toInt(),  // margin left
  13. //                        (event.rawY - v.height / 2).toInt(),  // margin top
  14. //                        (layoutParams.width - (event.rawX + v.width / 2)).toInt(),  // margin right
  15. //                        (layoutParams.height - (event.rawY + v.height / 2)).toInt()
  16. //                    )
  17. //                    v.layoutParams = layoutParams
  18. //                    v.performClick()
  19.                 }
  20.                 MotionEvent.ACTION_MOVE -> {
  21.                     val layoutParams = FrameLayout.LayoutParams(
  22.                         v.layoutParams.width,
  23.                         v.layoutParams.height
  24.                     )
  25.                     layoutParams.setMargins(
  26.                         (event.rawX - v.width / 2).toInt(),  // margin left
  27.                         (event.rawY - v.height / 2).toInt(),  // margin top
  28.                         (layoutParams.width - (event.rawX + v.width / 2)).toInt(),  // margin right
  29.                         (layoutParams.height - (event.rawY - v.height / 2)).toInt()
  30.                     )
  31.                     Log.d("TotalMargin", "${v.width/2} : ${v.height/2}")
  32.                     v.layoutParams = layoutParams
  33.                     v.performClick()
  34.                 }
  35.             }
  36.             true
  37.         }
  38.  
  39.  
  40. private fun mergeBitmap(file: File, collSign: Bitmap, custSign: Bitmap): Bitmap? {
  41.        return try {
  42.             val bitmap = loadAndRenderPdfToImage(file)
  43. //            val result = bitmap?.let { bitmapOverlayToCenter(it, collSign) }
  44.             val bitmap1Width = bitmap?.width
  45.             val bitmap1Height = bitmap?.height
  46.             val bitmap2Width = collSign.width
  47.             val bitmap2Height = collSign.height
  48.            val bitmap3Width = custSign.width
  49.            val bitmap3Height = custSign.height
  50. //        val marginLeft = (bitmap1Width * 0.5 - bitmap2Width * 0.5).toFloat()
  51. //        val marginTop = (bitmap1Height * 0.5 - bitmap2Height * 0.5).toFloat()
  52. //        val finalXColl: Float = (bitmap1Width - bitmap2Width).toFloat()
  53. //        val finalYColl: Float = (bitmap1Height - bitmap2Height).toFloat()
  54.  
  55. //            val finalXColl: Float = (bitmap1Width?.div(bitmap2Width))?.toFloat()!!
  56. //            val finalYColl: Float = (bitmap1Height?.div(bitmap2Height))?.toFloat()!!
  57. //           val finalXCust: Float = (bitmap1Width.div(bitmap3Width)).toFloat()
  58. //              val finalYCust: Float = (bitmap1Height.div(bitmap3Height)).toFloat()
  59.             val finalBitmap = Bitmap.createBitmap(bitmap1Width as Int, bitmap1Height as Int, bitmap.config)
  60.             val canvas = Canvas(finalBitmap)
  61.            bitmap.let { canvas.drawBitmap(it, Matrix(), null) }
  62. //           canvas.drawBitmap(collSign, xColl * finalXColl , yColl * finalYColl, null)
  63. //           canvas.drawBitmap(custSign, xCust * finalXCust, yCust * finalYCust, null)
  64.             canvas.drawBitmap(collSign, xColl * 1.5f , yColl * 1.82f, null)
  65.             canvas.drawBitmap(custSign, xCust * 1.95f, yCust * 1.82f, null)
  66. //            bitmap = bitmap?.let {
  67. //                Bitmap.createScaledBitmap(
  68. //                    it,
  69. //                    bitmap!!.width,
  70. //                    bitmap!!.height,
  71. //                    false
  72. //                )
  73. //            }
  74.  
  75.  
  76. //            val collSignBitmap = Bitmap.createScaledBitmap(collSign, 100, 100, false)
  77. //            val custSignBitmap = Bitmap.createScaledBitmap(custSign, 100, 100, false)
  78. //            val result = Bitmap.createBitmap(bitmap!!.width, bitmap.height, bitmap.config)
  79. //            val canvas = Canvas(result)
  80. //            canvas.drawBitmap(bitmap, Matrix(), null)
  81. //            val finalXColl: Float = bitmap.width.toFloat()/contentFinal.width.toFloat()
  82. //            val finalYColl: Float = bitmap.height.toFloat()/contentFinal.height.toFloat()
  83. //            canvas.drawBitmap(collSignBitmap, xColl, yColl, Paint())
  84. //            canvas.drawBitmap(custSignBitmap, xCust, yCust, Paint())
  85.  
  86.             Log.d("CreateBitmap", "Success $finalBitmap")
  87. //            content.visibility = View.GONE
  88.             collSign.recycle()
  89.             custSign.recycle()
  90. //            contentFinal.setImageBitmap(finalBitmap)
  91.             imageCollSign = null
  92.             imageCustSign = null
  93.            finalBitmap
  94.         } catch (e: Exception) {
  95.             e.printStackTrace()
  96.             Utils.showInfoMessage(this, "Error occurred while merging image", saveAndCreateImageToPdf)
  97.            null
  98.         }
  99.  
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement