焦点热议:Android使用SurfaceView实现签名板

2023-03-23 12:33:11 来源: 博客园


(资料图片)

SurfaceView使用

首先创建一个SurfaceViewSign类,继承SurfaceView类,继承 SurfaceHolder.Callback和Runnable接口,代码如下:

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    private Canvas canvas;    //子线程标志位    private boolean isDrawing;    //画笔    private Paint paint;    //路径    private Path path;​    private Bitmap bitmap;    private Canvas getCanvas;    /**     * 获取mCanvas里的bitmap     * */    public Bitmap getBitmap() {        getCanvas.drawColor(Color.WHITE);//画布背景色        getCanvas.drawPath(path, paint);        getCanvas.save();        getCanvas.restore();        return bitmap;        //region bitmap压缩到文件//        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/share_pic.png");// 保存到sdcard根目录下,文件名为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();        //添加回调        holder.addCallback(this);        path =new Path();        //初始化画笔        paint =new Paint();        paint.setStyle(Paint.Style.STROKE);        paint.setStrokeWidth(6);        paint.setAntiAlias(true);        paint.setColor(Color.RED);//画笔颜色        setFocusable(true);        setFocusableInTouchMode(true);        this.setKeepScreenOn(true);​​    }    //Surface的生命周期    @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{            //锁定画布并返回画布对象            canvas = holder.lockCanvas();            canvas.drawColor(Color.WHITE);//设置画布背景色            canvas.drawPath(path, paint);  //画线​        }catch (Exception e){        }finally {            if(canvas !=null)                holder.unlockCanvasAndPost(canvas);//解锁        }    }​    /**     * 绘制触摸滑动路径     * @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;    }​    /**     * 清屏     * @return true     */    public boolean reDraw(){        path.reset();        return true;    }​​}​

然后创建一个测试Activity,然后编写他的页面代码如下:

                  

然后在activity里编写点击事件如下:

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

这样就实现了简单的签名,并且获取到了签名的图片,类型是bitmap。

效果图如下:

----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!

标签:

相关热词搜索:

[责任编辑:]

相关阅读

最近更新