【发布时间】:2021-02-22 04:06:15
【问题描述】:
我正在创建一个基本媒体库,其中一个功能是图像注释。我从 android 存储系统获取媒体文件,然后将其转换为位图。然后我从位图创建一个画布,并使用画布类的 drawLine 函数来允许基本注释。我目前遇到的问题是画布和图像大小不同,因此线条绘制在错误的位置。
这是创建位图的代码。
val bmpFactoryOptions = BitmapFactory.Options()
bmpFactoryOptions.inJustDecodeBounds = false
bmp = (BitmapFactory.decodeFile(it.mediaUrl)).rotate(270f)
alteredBitmap = Bitmap.createBitmap(
bmp!!.width, bmp!!
.height, bmp!!.config
)
然后我创建画布、绘画和矩阵对象。
canvas = Canvas(alteredBitmap!!)
paint = Paint()
paint!!.color = Color.GREEN
paint!!.strokeWidth = 5f
matrix = Matrix()
canvas!!.drawBitmap(bmp!!, matrix!!, paint)
然后我将 changedBitmap 设置为图像视图的图像
annotateIV!!.setImageBitmap(alteredBitmap)
最后一个出错的地方是 imageView 的 touchListener。
annotateIV!!.setOnTouchListener { view, motionEvent ->
when (motionEvent.action) {
ACTION_DOWN -> {
downx = motionEvent.x
downy = motionEvent.y
}
ACTION_MOVE -> {
upx = motionEvent.x
upy = motionEvent.y
canvas!!.drawLine(downx, downy, upx, upy, paint!!)
annotateIV!!.invalidate()
downx = upx
downy = upy
}
ACTION_UP -> {
upx = motionEvent.x
upy = motionEvent.y
canvas!!.drawLine(downx, downy, upx, upy, paint!!)
annotateIV!!.invalidate()
}
ACTION_CANCEL -> {
}
else -> {
}
}
return@setOnTouchListener true
}
我尝试以不同的方式创建位图,但经常遇到相同的缩放错误。 Image Example of Error 注意左上角绘制的线条。这些是在尝试在整个屏幕上绘制时创建的。
感谢任何人的关注,非常感谢任何帮助。
对位图的创建进行了调整。这会导致图像被放大,但至少在正确的位置绘制线条
bmp = (BitmapFactory.decodeFile(it.mediaUrl)).rotate(270f)
alteredBitmap = Bitmap.createBitmap(
bmp!!.width, bmp!!
.height, bmp!!.config
)
alteredBitmap = Bitmap.createScaledBitmap(alteredBitmap!!, annotateIV.width, annotateIV.height, false)
【问题讨论】:
标签: android image kotlin canvas bitmap