运行有问题或需要源码请点赞关注收藏后评论区留言~~~
一、跟踪滑动轨迹实现手写签名
手写签名的原理是把手机屏幕当作画板,把用户手指当作画笔,手指在屏幕上划来划去,屏幕就会显示手指的移动轨迹,就像画笔在画板上写字一样,实现手写签名需要结合绘图的路径工具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>
创作不易 觉得有帮助请点赞关注收藏~~~