0

我必须创建一个 PhotoEditor,它的工作方式更像 WhatsApp PhotoEditor,用于缩放和平移以及在其上绘制东西。

我已经使用这个库PhotoView实现了缩放和平移功能,我想在该库上进行绘制,为此我创建了一个自定义类 Drawable View。它可以完美地工作,直到点图像没有缩放,我想创建它,就像有人在缩放的图像上绘制一些东西然后缩小画布油漆也应该缩小,反之亦然。

下面是我创建的用于在 ImageView 上绘制的类。任何人都可以帮助或提示我实现此功能。这是我自定义的 DrawableView 类,用于在 imageView 上绘图。

class DrawableView : View {

var widthView = 0
var heightView = 0
private var isEditable = false
private var drawPath: Path? = null
private var drawPaint: Paint? = null
private var canvasPaint: Paint? = null
private var drawCanvas: Canvas? = null
private var canvasBitmap: Bitmap? = null
private val paintColor = Color.RED

constructor(context: Context?) : super(context) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    canvasPaint = Paint(Paint.DITHER_FLAG)
    setupDrawing()
}

constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
)

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    super.onSizeChanged(w, h, oldw, oldh)
    heightView = h
    widthView = w
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
    drawCanvas = Canvas(canvasBitmap!!)
}

private fun setupDrawing() {
    drawPath = Path()
    drawPaint = Paint()
    drawPaint!!.color = paintColor
    drawPaint!!.isAntiAlias = true
    drawPaint!!.isDither = true
    drawPaint!!.style = Paint.Style.STROKE
    drawPaint!!.strokeJoin = Paint.Join.ROUND
    drawPaint!!.strokeCap = Paint.Cap.ROUND
    drawPaint!!.strokeWidth = 10f
}

fun setDrawingEnabled(isEditable: Boolean) {
    this.isEditable = isEditable
}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    canvas.drawBitmap(canvasBitmap!!, 0f, 0f, canvasPaint)
    canvas.drawPath(drawPath!!, drawPaint!!)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
    if (isEditable) {
        val touchX = event.x
        val touchY = event.y
        when (event.action) {
            MotionEvent.ACTION_DOWN -> drawPath?.moveTo(touchX, touchY)
            MotionEvent.ACTION_MOVE -> drawPath?.lineTo(touchX, touchY)
            MotionEvent.ACTION_UP -> {
                drawPath?.lineTo(touchX, touchY)
                drawCanvas?.drawPath(drawPath!!, drawPaint!!)
                drawPath = Path()
            }
            else -> return false
        }
    } else {
        return false
    }
    invalidate()
    return true
}

}

这是一个放大的图像,我在其中画了一些东西。

在此处输入图像描述

缩小后,您会看到画布的位置停留在那里,我也想缩小

在此处输入图像描述

我从stackoverflow和互联网尝试了很多方法,没有发现任何有用的东西。在这方面需要帮助。

4

0 回答 0