Android:在ImageView上添加标记

时间:2019-03-26 16:02:03

标签: bitmap imageview android-imageview android-canvas android-photoview

我试图用图像(平面图)和标记创建视图。我需要在用户点击的位置添加标记。当用户缩放ImageView时,此标记应为另一个视图,不缩放;但当用户拖动视图或对其进行缩放时,该标记应停留在放置的坐标上。因此它应该像在Google Maps上一样工作:将大图像作为背景和选择器,用户可以将其放置在任何地方。 我尝试使用PhotoView enter link description here,因为它支持缩放,拖动。但是我无法在此配置中创建它。我试图通过左,右,上,下将constrait picker进行photoview。 Bu PhotoView修改其中的图像,并且在缩放或拖动它时,选择器会“飞”出屏幕。 然后,我尝试在PhotoView中将选取器与图像合并,但是这种方法不是很正确,因为当用户缩放图像时,选取器随之缩放(它是图像的一部分)。

那么,也许有人做这样的事情?

我的解决方案:

var lastX = -1F
var lastY = -1F
photoview.setOnPhotoTapListener { _, x, y ->
    lastX = (x * originalImage.width) - picker.width / 2
    lastY = (y * originalImage.height) - picker.height
    photoview.setImageBitmap(originalImage.mergeWith(picker, lastX, lastY))
}
photoview.setOnScaleChangeListener { _, _, _ ->
    if (lastX != -1F && lastY != -1F) {
        photoview.setImageBitmap(originalImage.mergeWith(picker, lastX, lastY))
    }
}

用于合并图像和选择器的功能:

fun Bitmap.mergeWith(bp: Bitmap, x: Float, y: Float): Bitmap {
    val result = createBitmap(
        if (width > bp.width) width else bp.width,
        if (height > bp.height) height else bp.height,
        config
    )
    val canvas = Canvas(result)
    canvas.drawBitmap(this, 0F, 0F, null)
    canvas.drawBitmap(bp, x, y, null)
    return result
}

0 个答案:

没有答案
相关问题