简体   繁体   中英

Android - I'm trying to draw a Path on a Canvas so i can draw a signature on top of a Bitmap, but the signature is misaligned

I am trying to create a simple app where i can draw a signature over a pdf page that has been turned into a Bitmap. I'm successful on implementing the PDF feature but i'm having trouble implementing the SignaturePad feature in my app.

When i draw on my SignaturePad view, it draws exactly where i dragged on screen. Image: Signature on the drawing pad

However, when i display the resulting bitmap, the signature appears bigger and off-positioned. Image: Signature after displaying the resulting bitmap

I have tried multiple solution offerred to a closely-related problems like:

but none of them worked.

I have 0 (zero) experience of using the android.graphics library so i am clueless about what i'm doing wrong. Any help or suggestions are greatly appreciated!

My SignaturePad Class:

class SignaturePad(context: Context, attrs: AttributeSet): androidx.appcompat.widget.AppCompatImageView(context, attrs) {

    companion object{
        // To use for logging
        private const val TAG = "SignaturePad"
    }

    // this attribute store the original immutable bitmap
    private var originalBitmap: Bitmap? = null
    // this attribute store a mutable copy of the original bitmap
    private var mBitmap:Bitmap? = null

    private var path = Path()
    private val brush = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mCanvas: Canvas = Canvas()

    init{
        brush.isAntiAlias = true
        brush.color = Color.BLACK
        brush.style = Paint.Style.STROKE
        brush.strokeCap = Paint.Cap.ROUND
        brush.strokeJoin = Paint.Join.ROUND
        brush.strokeWidth = 5f
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        event?.let{
            val pointX:Float = it.x
            val pointY:Float = it.y

            when(it.action){
                MotionEvent.ACTION_DOWN -> {
                    path.moveTo(pointX, pointY)
                    return true
                }
                MotionEvent.ACTION_MOVE -> {
                    path.lineTo(pointX, pointY)
                }
                MotionEvent.ACTION_UP -> {
                    // do nothing
                }
                else -> return false
            }
            postInvalidate()
            return false
        }
        return super.onTouchEvent(event)
    }

    // function is overridden so i can store the bitmap into an attribute and use it on the canvas
    override fun setImageBitmap(bm: Bitmap?) {
        bm?.let{
            originalBitmap = bm
            mBitmap= originalBitmap!!.copy(Bitmap.Config.ARGB_8888, true)

            mCanvas = Canvas()
            mCanvas.setBitmap(mBitmap!!)
        }
        super.setImageBitmap(bm)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (mBitmap == null) {
            Log.e(TAG,"Bitmap is null!")
            mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        }
        mCanvas = Canvas()
        mCanvas.setBitmap(mBitmap)
        mCanvas.drawColor(Color.TRANSPARENT)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let{
            it.drawPath(path, brush)
            mCanvas.drawPath(path, brush)
        }
    }

    fun getCanvas():Canvas?{
        return mCanvas
    }

    fun getBitmap():Bitmap?{
        return mBitmap
    }

}

My custom SignaturePad Widget on an Activity:

    <com.fca.signpdf.view.SignaturePad
        android:id="@+id/signaturepad"
        android:background="@color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/buttonbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:minHeight="300dp"
        android:scaleType="fitCenter"/>

I figured it out. I logged the width and the height of my custom view on the overridden setImageBitmap function. Both returns zero. So i logged them again in the onLayout function, and both of them returns the actual width and height of my custom view.

Finally, i decided to use drawBitmap() instead of setBitmap() to resize the bitmap and moved a couple of lines from the setImageBitmap function to the bottom of onLayout function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM