Android手写签名绘制,并保存成bitmap

这是一个基础的自定义view,供大家参考
先看效果:
演示demo
要实现手写签名,我们的关键是实现一个自定义view,可以绘制出轨迹,这里,我起名为SignView,并使之继承View,并继承接口View.OnTouchListener,并建立一些变量

public class SignView extends View implements View.OnTouchListener{
    Bitmap bitmap=null;
    Path path;
    Rect boundary;
    Canvas canvas1;
    boolean isdraw;//用于判断路径是否为空
    public int bound,stroke;//用来提供给Activity设置使用
    private int width,height;
}

实现自定义view,至少要重写ondraw和构造方法,这里我们把4个构造方法全部写出来,并在其中加一些初始化的操作

   public SignView(Context context) {
        super(context);
        init();
    }

    public SignView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

在init中,我们要初始化我们的绘制轨迹,画笔的粗细,还有set接口,可能还会用到边界,我们都把他们初始化一下

 private void init() {
        path=new Path();
        isdraw=false;
        stroke=8;
        bound=8;
        setOnTouchListener(this);
    }

我们这里不需要重写onMeasure

  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

我们重写一下onLayout,用来设置边界和bitmap的大小,注意:onLayout中一定可以获取到getWidth()和getHeight()

 @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width=getWidth();
        height=getHeight();
        bitmap = Bitmap.createBitmap(width-bound, height-bound, Bitmap.Config.ARGB_8888);
        canvas1=new Canvas(bitmap);
        boundary=new Rect(bound,bound,width-bound,height-bound);
    }

接着我们运行onDraw,代码很简单,就是把之前的path和边框画出来

   @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(stroke);
        canvas.drawPath(path,paint);
        canvas1.drawPath(path,paint);
        canvas.drawRect(boundary,paint);
    }

我们继承了View.OnTouchListener,自然需要重写onTouch

 @Override
    public boolean onTouch(View v, MotionEvent event) {
        isdraw=true;
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN://当手指按下的时候触发
                path.moveTo(event.getX(),event.getY());
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE://当手指移动的时候触发
                path.lineTo(event.getX(),event.getY());
                invalidate();
                break;
        }
        return true;
    }

剩下的就是动态设置边框和画笔粗细了,这部分可有可无,主要是方便别人调用我们的自定义view

 public float getBound() {
        return bound;
    }

    public void setBound(int bound) {
        this.bound = bound;
    }

    public void setStroke(int stroke) {
        this.stroke = stroke;
    }

    public float getStroke() {
        return stroke;
    }

我们还需要返回bitmap

 public Bitmap getBitmap(){
        if(!isdraw)
            return null;
        return bitmap;
    }

还需要清空画布

 public void clear(){
        path.reset();
        bitmap = Bitmap.createBitmap(width-bound, height-bound, Bitmap.Config.ARGB_8888);
        canvas1=new Canvas(bitmap);
        invalidate();
    }

如此一来,我们的自定义view就完美了。
接着我们来看如何使用它

首先,在xml中创建他
<com.mafanwei.myapplication.SignView android:layout_marginTop="16dp" android:id="@+id/signview" android:layout_width="match_parent" android:layout_height="200dp" />
接着,在Activity中绑定他
SignView signView=findViewById(R.id.signview);

当我们需要他返回bitmap的时候,只需运行
signView.getBitmap()
当我们需要清空画布的时候,只需运行
signView.clear();

是不是很简单呢?
快去实现吧!

补充

将bitmap保存到本地

完整代码下载

android电子签名,屏幕上手写签名 搜集很多资料,项目能够完美运行,拿来即可使用,整理备用 应用场景: 就是在屏幕是用手写字,然后保存图片,简称就是电子签名,可以用在手机上签合同,等技术。 使用技术: 使用了接口回调,绘制之后给用户去操作 自定义Dialog,在dialog上画图,给dialog设置主题,dialog的宽高设置为手机屏幕的宽高充满全屏 注意在计算高度的时候记得减去通知栏的高度 注意把画布的背景设置为白色,不然点击缩略图查看的时候是全黑色 参考如下资料: http://hbxflihua.iteye.com/blog/1512765 http://www.jianshu.com/p/c4f017603413 https://github.com/gcacace/android-signaturepad http://download.csdn.net/download/mmlinux/7687091 1,android 如何让自定义dialog的宽度跟屏幕的宽度一样? 在你dialog.show();后面加上 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.width = (int)(display.getWidth()); //设置宽度 dialog.getWindow().setAttributes(lp); 2,如何获取通知栏的高度? public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } 3,如何对图片进行压缩? http://blog.sina.com.cn/s/blog_497f718e0100sl13.html http://www.cnblogs.com/Soprano/articles/2577152.html
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值