android实现手写签名功能
程序员文章站
2022-05-08 13:15:01
本文实例为大家分享了android手写签名展示的具体代码,供大家参考,具体内容如下
代码简单,就不发demo了,直接贴代码
package com.xx;
impo...
本文实例为大家分享了android手写签名展示的具体代码,供大家参考,具体内容如下
代码简单,就不发demo了,直接贴代码
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; } } }
布局文件:
<?xml version="1.0"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <imageview android:id="@+id/iv_sign" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center" android:layout_marginbottom="3dp" android:layout_weight="1" android:background="@color/white" /> <framelayout android:id="@+id/fl_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/white" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/bottom_bar" android:paddingtop="3dp" > <button android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" /> <button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="清除" /> </linearlayout> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。