繁体   English   中英

Android - 我正在尝试在 Canvas 上绘制路径,因此我可以在 Bitmap 上绘制签名,但签名未对齐

[英]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

我正在尝试创建一个简单的应用程序,我可以在其中在已变成 Bitmap 的 pdf 页面上绘制签名。 我成功实现了 PDF 功能,但在我的应用程序中实现 SignaturePad 功能时遇到问题。

当我在我的 SignaturePad 视图上绘图时,它会准确地绘制我在屏幕上拖动的位置。 图片:绘图板上的签名

但是,当我显示生成的 bitmap 时,签名会显得更大且偏离位置。 图片:显示结果 bitmap 后的签名

我尝试了多种解决方案来解决密切相关的问题,例如:

但他们都没有工作。

我有使用 android.graphics 库的 0(零)经验,所以我对自己做错了什么一无所知。 非常感谢任何帮助或建议!

我的 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
    }

}

我在 Activity 上的自定义 SignaturePad 小部件:

    <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"/>

我想到了。 我在覆盖的setImageBitmap function 上记录了自定义视图的宽度和高度。 两者都返回零。 所以我再次将它们记录在onLayout function 中,它们都返回了我的自定义视图的实际宽度和高度。

最后,我决定使用drawBitmap()而不是setBitmap()来调整 bitmap 的大小,并将几行从setImageBitmap function 移动到onLayout ZC1C425268E68385D1AB4ZA74F 的底部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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