Android 图像注释位图问题
Android Image Annotation Bitmap issue
我正在创建一个基本的媒体库,其中一个功能是图像注释。我从 android 存储系统获取媒体文件,然后将其转换为位图。然后我从位图中创建一个 canvas 并使用 canvas class 的 drawLine 函数来允许基本注释。我目前遇到的问题是 canvas 和图像大小不一样,所以线条绘制在错误的位置。
这是创建位图的代码。
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 = Canvas(alteredBitmap!!)
paint = Paint()
paint!!.color = Color.GREEN
paint!!.strokeWidth = 5f
matrix = Matrix()
canvas!!.drawBitmap(bmp!!, matrix!!, paint)
然后我将 alteredBitmap 设置为图像视图的图像
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)
您的 Canvas
大小为 Bitmap
,可能比屏幕尺寸大很多。您正在将 Bitmap
设置为 annotateIV
,我假设它是 ImageView
并设置了一些 scaleType
以适应整个 Bitmap
内部视图。这仍然没有实际调整大小 Bitmap
/Canvas
,它只是一个“预览”
为了解决这个问题,您应该将 Bitmap
缩放到 ImageView
的大小,或者将 factor/ratio 的值设为 bitmapSize/imageViewSize 并将所有 motionEvent.x
和 motionEvent.y
由它
顺便说一句。当每个 MotionEvent
到达时调用 invalidate()
不是有效的方法...这就是为什么你的线不是圆的,它显然是用直线制成的
我正在创建一个基本的媒体库,其中一个功能是图像注释。我从 android 存储系统获取媒体文件,然后将其转换为位图。然后我从位图中创建一个 canvas 并使用 canvas class 的 drawLine 函数来允许基本注释。我目前遇到的问题是 canvas 和图像大小不一样,所以线条绘制在错误的位置。
这是创建位图的代码。
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 = Canvas(alteredBitmap!!)
paint = Paint()
paint!!.color = Color.GREEN
paint!!.strokeWidth = 5f
matrix = Matrix()
canvas!!.drawBitmap(bmp!!, matrix!!, paint)
然后我将 alteredBitmap 设置为图像视图的图像
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)
您的 Canvas
大小为 Bitmap
,可能比屏幕尺寸大很多。您正在将 Bitmap
设置为 annotateIV
,我假设它是 ImageView
并设置了一些 scaleType
以适应整个 Bitmap
内部视图。这仍然没有实际调整大小 Bitmap
/Canvas
,它只是一个“预览”
为了解决这个问题,您应该将 Bitmap
缩放到 ImageView
的大小,或者将 factor/ratio 的值设为 bitmapSize/imageViewSize 并将所有 motionEvent.x
和 motionEvent.y
由它
顺便说一句。当每个 MotionEvent
到达时调用 invalidate()
不是有效的方法...这就是为什么你的线不是圆的,它显然是用直线制成的