使用Kotlin实现电子签名
【摘要】 Kotlin Demo 使用Kotlin实现了电子签名,用Kotlin写Android应用真是舒服。 我自定义了一个SignatureView:
package com.wong.testp
import android.annotation.SuppressLint
import android.content.Context
import android.gra...
Kotlin Demo
使用Kotlin实现了电子签名,用Kotlin写Android应用真是舒服。
我自定义了一个SignatureView:
package com.wong.testp
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Base64
import android.view.MotionEvent
import android.view.View
import java.io.ByteArrayOutputStream
import kotlin.math.abs
class SignatureView : View { companion object { fun saveBitmapToBase64(bitmap: Bitmap):String{ val baos = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos) baos.flush() baos.close() return Base64.encodeToString(baos.toByteArray(),Base64.NO_WRAP) } fun base64ToBitmap(base64:String):Bitmap{ val bytes = Base64.decode(base64,Base64.NO_WRAP) return BitmapFactory.decodeByteArray(bytes,0,bytes.size) } } var mPathPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) // 笔划画笔 lateinit var canvas: Canvas // 画布 private lateinit var mPath: Path // 记录在屏幕上触摸移动时的点组成的路径 private var mPreX: Float = 0.0f // 记录x坐标 private var mPreY: Float = 0.0f // 记录y坐标 private val touchTolerance:Float = 4F // 两点之间的差 init { mPathPaint.isAntiAlias = true mPathPaint.isDither = true mPathPaint.style = Paint.Style.STROKE mPathPaint.strokeJoin = Paint.Join.ROUND mPathPaint.strokeCap = Paint.Cap.ROUND mPathPaint.strokeWidth = 10F mPathPaint.color = Color.BLACK } constructor(context: Context) : super(context) {} constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {} constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {} constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {} @SuppressLint("DrawAllocation") override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) mPath = Path() canvas = Canvas() } @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(event: MotionEvent?): Boolean { when(event?.action){ MotionEvent.ACTION_DOWN->{ mPreX = event.x mPreY = event.y mPath.moveTo(mPreX, mPreY) } MotionEvent.ACTION_MOVE->{ val x = event.x val y = event.y val dx = abs(mPreX-x) val dy = abs(mPreY - y) if(dx >= touchTolerance || dy >= touchTolerance){ mPath.quadTo(mPreX,mPreY,(x+mPreX)/2,(y+mPreY)/2) mPreX = event.x mPreY = event.y } } MotionEvent.ACTION_UP->{ mPath.lineTo(event.x,event.y) canvas.drawPath(mPath,mPathPaint) } } invalidate() return true } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.drawPath(mPath,mPathPaint) } fun reset(){ mPath = Path() canvas = Canvas() invalidate() } val mBitmap:Bitmap? get() { val bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) if(background != null){ background.draw(canvas) }else{ canvas.drawColor(Color.WHITE) draw(canvas) } return bitmap }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
使用伴生对象提供了两个方法,一个是将Bitmap转Base64,另一个是将Base64字符串转成Bitmap。
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/110351909
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)