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天前
|
JSON 编译器 开发工具
VS Code阅读Android源码
VS Code阅读Android源码
11 1
|
25天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
52 1
|
25天前
|
Java Android开发
Android反编译查看源码
Android反编译查看源码
25 0
|
1月前
|
存储 小程序 API
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
103 0
|
1月前
|
小程序 开发工具 git
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
|
1月前
|
JavaScript Android开发
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
26 0
|
4天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
7 0
|
4天前
|
Android开发
Android源代码定制:添加customize.mk文件进行分项目和分客户的定制
Android源代码定制:添加customize.mk文件进行分项目和分客户的定制
3 0
|
5天前
|
测试技术 Android开发
Android App获取不到pkgInfo信息问题原因
Android App获取不到pkgInfo信息问题原因
14 0