电子签名

这篇博客介绍了如何在Android项目中实现电子签名功能,包括布局设置、SignatureView的创建以及在Activity中的应用步骤。
摘要由CSDN通过智能技术生成

之前在网上借鉴一个demo但后来找不到了,就把自己项目放出来,记录一下。

一、首先是布局 

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="65dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">

        <charging.jingmi.nanjing.com.filemanagement.views.SignatureView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:id="@+id/signature_pad"
            android:enabled="false"
            android:background="@color/gray_background"/>

        <TextView android:id="@+id/tv_sign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="签名区"
            android:textColor="@color/black"
            android:layout_centerInParent="true"/>

        <LinearLayout
            android:id="@+id/buttons_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_below="@+id/signature_pad"
            android:orientation="horizontal">

            <Button
                android:id="@+id/clear_button"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:enabled="false"
                android:text="清除"
                android:textColor="@color/colorAccent"
                android:textSize="15sp" />

            <Button
                android:id="@+id/save_button"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:enabled="false"
                android:text="保存"
                android:textColor="@color/colorAccent"
                android:textSize="15sp" />
        </LinearLayout>

    </RelativeLayout>

    <Button android:id="@+id/btn_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"
        android:text="转base64"
        android:textColor="@color/white"
        android:background="@drawable/side_nav_bar"/>

二、View文件夹中新建SignatureView定义画笔

public class SignatureView extends View {
    // View state
    private List<TimedPoint> mPoints;
    private boolean mIsEmpty;
    private float mLastTouchX;
    private float mLastTouchY;
    private float mLastVelocity;
    private float mLastWidth;
    private RectF mDirtyRect;

    // Configurable parameters
    private int mMinWidth;
    private int mMaxWidth;
    private float mVelocityFilterWeight;
    private OnSignedListener mOnSignedListener;

    private Paint mPaint = new Paint();
    private Path mPath = new Path();
    private Bitmap mSignatureBitmap = null;
    private Canvas mSignatureBitmapCanvas = null;

    public SignatureView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SignatureView, 0, 0);

        // Configurable parameters
        try {
            mMinWidth = a.getDimensionPixelSize(R.styleable.SignatureView_minWidth, convertDpToPx(3));
            mMaxWidth = a.getDimensionPixelSize(R.styleable.SignatureView_maxWidth, convertDpToPx(20));
            mVelocityFilterWeight = a.getFloat(R.styleable.SignatureView_velocityFilterWeight, 0.9f);
            mPaint.setColor(a.getColor(R.styleable.SignatureView_penColor, Color.BLACK));
        } finally {
            a.recycle();
        }

        // Fixed parameters
        mPaint.setAntiAlias(true);



        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        //mPaint.setStyle(Paint.Style.FILL);  //画笔风格
//		mPaint.setAntiAlias(true);          //抗锯齿
//		mPaint.setStrokeWidth(10);           //画笔粗细
//		mPaint.setTextSize(100);             //绘制文字大小,单位px
        //mPaint.setStrokeWidth(Paint.S);
        // Dirty rectangle to update only the changed portion of the view
        mDirtyRect = new RectF();

        clear();
    }

    /**
     * Set the pen color from a given resource. If the resource is no
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值