Android App实战项目之实现手写签名APP功能(附源码,简单易懂 可直接实用)

简介: Android App实战项目之实现手写签名APP功能(附源码,简单易懂 可直接实用)

运行有问题或需要源码请点赞关注收藏后评论区留言~~~

一、跟踪滑动轨迹实现手写签名

手写签名的原理是把手机屏幕当作画板,把用户手指当作画笔,手指在屏幕上划来划去,屏幕就会显示手指的移动轨迹,就像画笔在画板上写字一样,实现手写签名需要结合绘图的路径工具Path,具体实现步骤如下

1:按下手指时 调用Path对象的moveTo方法 将路径起点移动到触摸点

2:移动手指时 调用Path对象的quadTo方法 记录本次触摸点与上次触摸点之间的路径

3:移动手指或手指提起时,调用Canvas对象的drawPath方法,将本次触摸轨迹绘制在画布上

效果如下

点击开始签名即可开始签名,模拟机就拿鼠标画就可以,真机测试效果会更好

画完后点击重置则会清空

点击回退则会清空上一次的操作

点击结束签名后下方就会出现签名的内容,点击保存图片文件就可以将图片保存至相册用于下次使用

代码如下

Java类

package com.example.event;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.TextView;
@SuppressLint("DefaultLocale")
public class TouchSingleActivity extends AppCompatActivity {
    private TextView tv_touch; // 声明一个文本视图对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch_single);
        tv_touch = findViewById(R.id.tv_touch);
    }
    // 在发生触摸事件时触发
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 从开机到现在的毫秒数
        int seconds = (int) (event.getEventTime() / 1000);
        String desc = String.format("动作发生时间:开机距离现在%02d:%02d:%02d",
                seconds / 3600, seconds % 3600 / 60, seconds % 60);
        desc = String.format("%s\n动作名称是:", desc);
        int action = event.getAction(); // 获得触摸事件的动作类型
        if (action == MotionEvent.ACTION_DOWN) { // 按下手指
            desc = String.format("%s按下", desc);
        } else if (action == MotionEvent.ACTION_MOVE) { // 移动手指
            desc = String.format("%s移动", desc);
        } else if (action == MotionEvent.ACTION_UP) { // 松开手指
            desc = String.format("%s提起", desc);
        } else if (action == MotionEvent.ACTION_CANCEL) { // 取消手势
            desc = String.format("%s取消", desc);
        }
        desc = String.format("%s\n动作发生位置是:横坐标%f,纵坐标%f,压力为%f",
                desc, event.getX(), event.getY(), event.getPressure());
        tv_touch.setText(desc);
        return super.onTouchEvent(event);
    }
}

XML文件

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
      ch_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <Button
                    android:id="@+id/btn_begin_signature"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    anor/black"
                    android:textSize="17sp" />
                <Button
                    android:id="@+id/btn_reset_signature"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:text="重置"
                    android:textColor="@color/black"
                    android:textSize="17sp" />
                <Button
                    android:id="@+id/btn_revoke_signature"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:text="回退"
                    android:textColor="@color/black"
                    android:textSize="17sp" />
                <Button
                    android:id="@+id/btn_end_signature"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:text="结束签名"
                    android:textColor="@color/black"
                    android:textSize="17sp" />
            </LinearLayout>
            <com.example.event.widget.SignatureView
                android:id="@+id/view_signature"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@color/white"
                app:paint_color="#0000aa"
                app:stroke_width="5" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <Button
                    android:id="@+id/btn_save_signature"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="保存图片文件"
                    android:textColor="@color/black"
                    android:textSize="17sp" />
            </LinearLayout>
            <ImageView
                android:id="@+id/iv_signature_new"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@color/white" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
5天前
|
存储 Java API
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
93 0
|
3天前
|
移动开发 监控 供应链
JAVA智慧工厂制造生产管理MES系统,全套源码,多端展示(app、小程序、H5、台后管理端)
一开始接触MES系统,很多人会和博主一样,对MES细节的应用不了解,这样很正常,因为MES系统相对于其他系统来讲应用比较多!
15 1
JAVA智慧工厂制造生产管理MES系统,全套源码,多端展示(app、小程序、H5、台后管理端)
|
5天前
简洁大气APP下载单页源码
简洁大气APP下载单页源码,源码由HTML+CSS+JS组成,记事本打开源码文件可以进行内容文字之类的修改,双击html文件可以本地运行效果,也可以上传到服务器里面
11 0
简洁大气APP下载单页源码
|
5天前
|
Android开发
Android 获取签名信息
Android 获取签名信息
12 0
|
5天前
|
安全 Android开发
修改Android系统的签名
修改Android系统的签名
22 0
|
5天前
|
安全 Android开发
Android 系统签名
Android 系统签名
18 0
|
5天前
|
存储 JavaScript 前端开发
uni-app 从vue3项目创建到Pinia管理数据全局使用 持久化存储数据 详细教程
uni-app 从vue3项目创建到Pinia管理数据全局使用 持久化存储数据 详细教程
|
5天前
|
安全 Android开发 数据安全/隐私保护
Android 如何获取系统签名 并使用系统签名
Android 如何获取系统签名 并使用系统签名
16 2
|
5天前
浅蓝色的APP下载界面HMTL源码
浅蓝色的APP下载界面HMTL源码
28 0
浅蓝色的APP下载界面HMTL源码
|
5天前
|
人工智能 监控 数据可视化
Java智慧工地云平台源码带APP SaaS模式 支持私有化部署和云部署
智慧工地是指应用智能技术和互联网手段对施工现场进行管理和监控的一种工地管理模式。它利用传感器、监控摄像头、人工智能、大数据等技术,实现对施工现场的实时监测、数据分析和智能决策,以提高工地的安全性、效率和质量(技术架构:微服务+Java+Spring Cloud +UniApp +MySql)。
39 4
http://www.vxiaotou.com