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>

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

相关文章
|
6天前
|
安全 Android开发 Kotlin
Android经典实战之SurfaceView原理和实践
本文介绍了 `SurfaceView` 这一强大的 UI 组件,尤其适合高性能绘制任务,如视频播放和游戏。文章详细讲解了 `SurfaceView` 的原理、与 `Surface` 类的关系及其实现示例,并强调了使用时需注意的线程安全、生命周期管理和性能优化等问题。
33 7
|
3天前
|
编解码 前端开发 Android开发
Android经典实战之TextureView原理和高级用法
本文介绍了 `TextureView` 的原理和特点,包括其硬件加速渲染的优势及与其他视图叠加使用的灵活性,并提供了视频播放和自定义绘制的示例代码。通过合理管理生命周期和资源,`TextureView` 可实现高效流畅的图形和视频渲染。
24 12
|
6天前
|
Android开发 容器
Android经典实战之如何获取View和ViewGroup的中心点
本文介绍了在Android中如何获取`View`和`ViewGroup`的中心点坐标,包括计算相对坐标和屏幕上的绝对坐标,并提供了示例代码。特别注意在视图未完成测量时可能出现的宽高为0的问题及解决方案。
18 7
|
10天前
|
调度 Android开发 UED
Android经典实战之Android 14前台服务适配
本文介绍了在Android 14中适配前台服务的关键步骤与最佳实践,包括指定服务类型、请求权限、优化用户体验及使用WorkManager等。通过遵循这些指南,确保应用在新系统上顺畅运行并提升用户体验。
25 6
|
8天前
|
Android开发
Android经典实战之Textview文字设置不同颜色、下划线、加粗、超链接等效果
本文介绍了 `SpannableString` 在 Android 开发中的强大功能,包括如何在单个字符串中应用多种样式,如颜色、字体大小、风格等,并提供了详细代码示例,展示如何设置文本颜色、添加点击事件等,助你实现丰富文本效果。
43 3
|
8天前
|
Android开发 UED 开发者
Android经典实战之WindowManager和创建系统悬浮窗
本文详细介绍了Android系统服务`WindowManager`,包括其主要功能和工作原理,并提供了创建系统悬浮窗的完整步骤。通过示例代码,展示了如何添加权限、请求权限、实现悬浮窗口及最佳实践,帮助开发者轻松掌握悬浮窗开发技巧。
21 1
|
9天前
|
API Android开发 数据安全/隐私保护
Android经典实战之窗口和WindowManager
本文介绍了Android开发中“窗口”的基本概念及其重要性。窗口是承载用户界面的基础单位,而`WindowManager`系统服务则负责窗口的创建、更新和移除等操作。了解这些概念有助于开发复杂且用户体验良好的应用。
14 2
|
13天前
|
监控 Java API
Android经典实战之OkDownload:一个经典强大的文件下载开源库,支持断点续传
本文介绍的 OkDownload 是一个专为 Android 设计的开源下载框架,支持多线程下载、断点续传和任务队列管理等功能,具备可靠性、灵活性和高性能特点。它提供了多种配置选项和监听器,便于开发者集成和扩展。尽管已多年未更新,但依然适用于大多数文件下载需求。
62 1
|
14天前
|
算法 安全 数据安全/隐私保护
Android经典实战之常见的移动端加密算法和用kotlin进行AES-256加密和解密
本文介绍了移动端开发中常用的数据加密算法,包括对称加密(如 AES 和 DES)、非对称加密(如 RSA)、散列算法(如 SHA-256 和 MD5)及消息认证码(如 HMAC)。重点讲解了如何使用 Kotlin 实现 AES-256 的加密和解密,并提供了详细的代码示例。通过生成密钥、加密和解密数据等步骤,展示了如何在 Kotlin 项目中实现数据的安全加密。
53 1
|
14天前
|
存储 安全 API
Android经典实战之存储方案对比:SharedPreferences vs MMKV vs DataStore
本文介绍了 Android 开发中常用的键值对存储方案,包括 SharedPreferences、MMKV 和 DataStore,并对比了它们在性能、并发处理、易用性和稳定性上的特点。通过实际代码示例,帮助开发者根据项目需求选择最适合的存储方案,提升应用性能和用户体验。
36 1