本文实例为大家分享了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,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,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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的android实现手写签名功能全部内容,希望文章能够帮你解决android实现手写签名功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)