Android uses SurfaceView to implement signature pad

SurfaceView uses

First create a SurfaceViewSign class, inherit the SurfaceView class, inherit the SurfaceHolder.Callback and Runnable interfaces, the code is as follows:

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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
?
public class SurfaceViewSign extends SurfaceView implements SurfaceHolder. Callback, Runnable {
?
    //SurfaceHolder
    private SurfaceHolder holder;
    // Canvas for drawing
    private Canvas canvas;
    // sub thread flag bit
    private boolean isDrawing;
    //brush
    private Paint paint;
    //path
    private Path path;
?
    private Bitmap bitmap;
    private Canvas getCanvas;
    /**
     * Get the bitmap in mCanvas
     * */
    public Bitmap getBitmap() {
        getCanvas.drawColor(Color.WHITE);//canvas background color
        getCanvas. drawPath(path, paint);
        getCanvas. save();
        getCanvas. restore();
        return bitmap;
        //region bitmap compressed to a file
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/share_pic.png");// Save to the root directory of sdcard, the file name is share_pic.png
// FileOutputStream fos = null;
// try {
// fos = new FileOutputStream(file);
// bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);
//
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e. printStackTrace();
// }
// try {
// fos. close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e. printStackTrace();
// }
        //endregion
    }
?
    public SurfaceViewSign(Context context) {
        super(context);
        initView();
    }
?
?
    public SurfaceViewSign(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
?
    public SurfaceViewSign(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
?
    private void initView() {
        bitmap = Bitmap.createBitmap(1000,1000, Bitmap.Config.ARGB_8888);
        getCanvas = new Canvas(bitmap);
        holder = getHolder();
        //add callback
        holder. addCallback(this);
        path = new Path();
        //Initialize the brush
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(6);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);//Brush color
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
?
?
    }
    //Surface life cycle
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isDrawing = true;
        new Thread(this).start();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
?
    }
?
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;
?
    }
?
    @Override
    public void run() {
        long start = System. currentTimeMillis();
        while(isDrawing){
            draw();
            long end = System. currentTimeMillis();
            if(end-start<100){
                try {
                    Thread. sleep(100-end + start);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
?
    private void draw() {
        try {
            //Lock the canvas and return the canvas object
            canvas = holder. lockCanvas();
            canvas.drawColor(Color.WHITE);//Set canvas background color
            canvas.drawPath(path, paint); // draw line
?
        }catch (Exception e){
        } finally {
            if(canvas !=null)
                holder.unlockCanvasAndPost(canvas);//unlock
        }
    }
?
    /**
     * Draw touch slide path
     * @param event MotionEvent
     * @return true
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x=(int) event. getX();
        int y = (int) event. getY();
        switch (event. getAction()){
            case MotionEvent. ACTION_DOWN:
                path.moveTo(x,y);
                break;
            case MotionEvent. ACTION_MOVE:
                path.lineTo(x,y);
                break;
            case MotionEvent. ACTION_UP:
                break;
        }
        return true;
    }
?
    /**
     * clear screen
     * @return true
     */
    public boolean reDraw(){
        path.reset();
        return true;
    }
?
?
}
?</code>
Then create a test Activity, and write his page code as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:background="@color/white"
    tools:ignore="MissingClass">
    <com.kiba.test.control.surfaceview.SurfaceViewSign
        android:id="@ + id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
    <View
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="@color/red"></View>
    <ImageView
       android:id="@ + id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        ></ImageView>
 
     
</LinearLayout>

Then write the click event in the activity as follows:

@SingleClick
    @OnClick(R. id. imageView)
    public void ImageClick()
    {
        Bitmap bitmap = sv. getBitmap();
        imageView.setImageBitmap(bitmap);
        imageView2.setImageBitmap(bitmap);
    }

In this way, a simple signature is realized, and the signed image is obtained, and the type is bitmap.

The renderings are as follows:

————————————————– ————————————————–

Note: This article is original, any form of reprint please contact the author for authorization and indicate the source!
If you think this article is not bad, please click 【Recommend】 below, thank you very much!