android中怎么实现一个手写签名功能
本篇文章给大家分享的是有关android中怎么实现一个手写签名功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
成都创新互联公司专业网站设计制作、成都网站建设,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文发布平台等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。
具体内容如下
package com.xx; import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.FrameLayout;import android.widget.ImageView;import com.xx.R; /** * Description: 签名类 * Copyright: Copyright (c)2018 * Company: * author: Corwin * version: 1.0 * date: 2018/9/5 18:32 * Modification History: * Date Author Version Description * ------------------------------------------------------------------ * 2018/9/5 Corwin 1.0 1.0 Version */public class SignatureActivity extends AppCompatActivity { private ImageView imageSign; private SignatureView mView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signature); imageSign = findViewById(R.id.iv_sign); FrameLayout frameLayout = findViewById(R.id.fl_view); mView = new SignatureView(this); frameLayout.addView(mView); mView.requestFocus(); Button btnClear = findViewById(R.id.btn_clear); btnClear.setOnClickListener((v) -> { mView.clear(); }); Button btnOk = findViewById(R.id.btn_ok); btnOk.setOnClickListener((v) -> { Bitmap imageBitmap = mView.getCachebBitmap(); imageSign.setImageBitmap(imageBitmap); }); } /** * 自定义签名控件 */ class SignatureView extends View { //画笔 private Paint paint; //画布 private Canvas cacheCanvas; //位图 private Bitmap cachebBitmap; //图片保存路径 private Path path; //位图缓存 public Bitmap getCachebBitmap() { return cachebBitmap; } public SignatureView(Context context) { super(context); init(); } /** * 初始化 */ private void init() { //设置画笔 paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); path = new Path(); //创建位图 cachebBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); //用自定义位图构建画布 cacheCanvas = new Canvas(cachebBitmap); //设置画布为白色 cacheCanvas.drawColor(Color.WHITE); } /** * 清除画板,重置画笔 */ public void clear() { if (cacheCanvas != null) { paint.setColor(Color.WHITE); cacheCanvas.drawPaint(paint); paint.setColor(Color.BLACK); cacheCanvas.drawColor(Color.WHITE); invalidate(); } } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(cachebBitmap, 0, 0, null); canvas.drawPath(path, paint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0; int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0; if (curW >= w && curH >= h) { return; } if (curW < w) curW = w; if (curH < h) curH = h; Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888); Canvas newCanvas = new Canvas(); newCanvas.setBitmap(newBitmap); if (cachebBitmap != null) { newCanvas.drawBitmap(cachebBitmap, 0, 0, null); } cachebBitmap = newBitmap; cacheCanvas = newCanvas; } private float cur_x, cur_y; @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { cur_x = x; cur_y = y; path.moveTo(cur_x, cur_y); break; } case MotionEvent.ACTION_MOVE: { path.quadTo(cur_x, cur_y, x, y); cur_x = x; cur_y = y; break; } case MotionEvent.ACTION_UP: { cacheCanvas.drawPath(path, paint); path.reset(); break; } } invalidate(); return true; } }}
布局文件:
以上就是android中怎么实现一个手写签名功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网页题目:android中怎么实现一个手写签名功能
转载源于:http://myzitong.com/article/jjdohj.html