手写签名自定义View

效果图

自定义View代码 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.apex.zhxtapp.staff.utils.FileUtils;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 手写签字版View
 */
public class SignView extends View {

    //默认的边距
    private final float mBorder = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            16, getResources().getDisplayMetrics());
    //文本字体大小
    private final float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
            40, getResources().getDisplayMetrics());
    //布局方向是否横向
    private boolean mOrientationHor = true;
    private int mWidth, mHeight;
    //基准线画笔和路径
    private Paint mPaintBorder;
    private Path mPathBaseLine;
    private final float[] mDashFloat = new float[]{6, 3, 6, 3};
    private final DashPathEffect mEffectBorder = new DashPathEffect(mDashFloat, 0);
    //画笔和路径
    private Paint paint;
    private Path mPath;
    private float preX, preY;
    //绘制的内容保存
    private Bitmap mBitmapPic;
    private Canvas mCanvasPic;

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

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

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

    //初始化画笔和路径
    private void initPaintPath() {

        mPaintBorder = new Paint();
        mPaintBorder.setAntiAlias(true);
        mPaintBorder.setColor(Color.parseColor("#E0E0E0"));
        mPaintBorder.setTextSize(mTextSize);
        mPaintBorder.setTextAlign(Paint.Align.CENTER);
        mPaintBorder.setStrokeWidth(2);

        mPathBaseLine = new Path();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);

        mPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initWidthHeight(w, h);
    }

    //控件尺寸变更处理
    private void initWidthHeight(int width, int height) {

        mOrientationHor = width > height;
        mWidth = width;
        mHeight = height;

        //测算基准线
        float fEndLineX = mWidth - mBorder;
        float fEndLineY = mHeight - mBorder;
        float fItemHor = (mWidth - mBorder * 2) / 13;
        float fItemVer = (mHeight - mBorder * 2) / 5;
        for (int i = 0; i < 14; i++) {

            float fItemX = mBorder + i * fItemHor;
            mPathBaseLine.moveTo(fItemX, mBorder);
            mPathBaseLine.lineTo(fItemX, fEndLineY);
        }

        for (int n = 0; n < 6; n++) {

            float fItemY = mBorder + n * fItemVer;
            mPathBaseLine.moveTo(mBorder, fItemY);
            mPathBaseLine.lineTo(fEndLineX, fItemY);
        }

        createBitmapSign();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        canvas.drawColor(Color.WHITE);
        //绘制边框线
        mPaintBorder.setStyle(Paint.Style.STROKE);
        mPaintBorder.setPathEffect(mEffectBorder);
        canvas.drawPath(mPathBaseLine, mPaintBorder);
        //绘制提示文案
        mPaintBorder.setPathEffect(null);
        mPaintBorder.setStyle(Paint.Style.FILL);
        String mStrHint = "签字区域";
        canvas.drawText(mStrHint, mWidth / 2, mHeight / 2 + (mTextSize / 3), mPaintBorder);
        //绘制签字路径
        canvas.drawPath(mPath, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mPath.moveTo(event.getX(), event.getY());
            preX = event.getX();
            preY = event.getY();

            saveCanvasPic();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            float endX = (preX + event.getX()) / 2;
            float endY = (preY + event.getY()) / 2;

            mPath.quadTo(preX, preY, endX, endY);

            preX = event.getX();
            preY = event.getY();

            saveCanvasPic();
            invalidate();
            return true;
        }
        return super.onTouchEvent(event);
    }

    //创建绘制保存的Bitmap
    private void createBitmapSign() {

        mBitmapPic = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mCanvasPic = new Canvas(mBitmapPic);
    }

    //保存回复绘制的内容
    private void saveCanvasPic() {

        if (mCanvasPic != null) {

            mCanvasPic.drawPath(mPath, paint);
            mCanvasPic.save();
            mCanvasPic.restore();
        }
    }

    //清空手写内容
    public void reset() {

        createBitmapSign();

        mPath.reset();
        invalidate();
    }

    //将绘制内容生成图片
    public boolean getSignPic() {

        File filePic = new File(FileUtils.getDirAvicStaff() + File.separator + "sign.png");
        if (filePic.exists()) {
            boolean bDel = filePic.delete();
            if (!bDel) {
                filePic = new File(FileUtils.getDirAvicStaff() + File.separator
                        + System.currentTimeMillis() + "_sign.png");
            }
        }
        try {
            return mBitmapPic.compress(Bitmap.CompressFormat.PNG, 100,
                    new FileOutputStream(filePic));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/nsacer/article/details/120204206