运行有问题或需要源码请点赞关注收藏后评论区留言~~~
手写签名的原理是把手机屏幕当作画板,把用户手指当作画笔,手指在屏幕上划来划去,屏幕就会显示手指的移动轨迹,就像画笔在画板上写字一样,实现手写签名需要结合绘图的路径工具Path,具体实现步骤如下
1:按下手指时 调用Path对象的moveTo方法 将路径起点移动到触摸点
2:移动手指时 调用Path对象的quadTo方法 记录本次触摸点与上次触摸点之间的路径
3:移动手指或手指提起时,调用Canvas对象的drawPath方法,将本次触摸轨迹绘制在画布上
效果如下
点击开始签名即可开始签名,模拟机就拿鼠标画就可以,真机测试效果会更好

画完后点击重置则会清空
点击回退则会清空上一次的操作


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

代码如下
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; // 声明一个文本视图对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_touch_single);tv_touch = findViewById(R.id.tv_touch);}// 在发生触摸事件时触发@Overridepublic 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);}}
android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" >ch_parent"android:layout_height="wrap_content"android:orientation="vertical" >
创作不易 觉得有帮助请点赞关注收藏~~~
上一篇:有哪些描述「去郊游」的句子?
下一篇:用无处不在的东西造句。