android实现手写签名功能

 更新时间:2019年11月29日 15:09:01   作者:黄俄白  
这篇文章主要为大家详细介绍了android实现手写签名功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android手写签名展示的具体代码,供大家参考,具体内容如下

代码简单,就不发demo了,直接贴代码

package com.xx;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.xx.R;
 
/**
 * Description: 签名类
 * Copyright:  Copyright (c)2018
 * Company:   
 * author:   Corwin
 * version:   1.0
 * date:    2018/9/5 18:32
 * Modification History:
 * Date     Author   Version   Description
 * ------------------------------------------------------------------
 * 2018/9/5  Corwin   1.0     1.0 Version
 */
public class SignatureActivity extends AppCompatActivity {
 
 private ImageView imageSign;
 private SignatureView mView;
 
 @Override public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_signature);
 
  imageSign = findViewById(R.id.iv_sign);
  FrameLayout frameLayout = findViewById(R.id.fl_view);
 
  mView = new SignatureView(this);
  frameLayout.addView(mView);
  mView.requestFocus();
 
  Button btnClear = findViewById(R.id.btn_clear);
  btnClear.setOnClickListener((v) -> {
    mView.clear();
  });
 
  Button btnOk = findViewById(R.id.btn_ok);
  btnOk.setOnClickListener((v) -> {
    Bitmap imageBitmap = mView.getCachebBitmap();
    imageSign.setImageBitmap(imageBitmap);
  });
 }
 
 /**
  * 自定义签名控件
  */
 class SignatureView extends View {
 
  //画笔
  private Paint paint;
 
  //画布
  private Canvas cacheCanvas;
 
  //位图
  private Bitmap cachebBitmap;
 
  //图片保存路径
  private Path path;
 
  //位图缓存
  public Bitmap getCachebBitmap() {
   return cachebBitmap;
  }
 
  public SignatureView(Context context) {
   super(context);
   init();
  }
 
  /**
   * 初始化
   */
  private void init() {
   //设置画笔
   paint = new Paint();
   paint.setAntiAlias(true);
   paint.setStrokeWidth(3);
   paint.setStyle(Paint.Style.STROKE);
   paint.setColor(Color.BLACK);
   path = new Path();
 
   //创建位图
   cachebBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
 
   //用自定义位图构建画布
   cacheCanvas = new Canvas(cachebBitmap);
   //设置画布为白色
   cacheCanvas.drawColor(Color.WHITE);
  }
 
  /**
   * 清除画板,重置画笔
   */
  public void clear() {
   if (cacheCanvas != null) {
    paint.setColor(Color.WHITE);
    cacheCanvas.drawPaint(paint);
    paint.setColor(Color.BLACK);
    cacheCanvas.drawColor(Color.WHITE);
    invalidate();
   }
  }
 
  @Override protected void onDraw(Canvas canvas) {
   canvas.drawBitmap(cachebBitmap, 0, 0, null);
   canvas.drawPath(path, paint);
  }
 
  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 
   int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
   int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
   if (curW >= w && curH >= h) {
    return;
   }
 
   if (curW < w) curW = w;
   if (curH < h) curH = h;
   Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
   Canvas newCanvas = new Canvas();
   newCanvas.setBitmap(newBitmap);
   if (cachebBitmap != null) {
    newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
   }
   cachebBitmap = newBitmap;
   cacheCanvas = newCanvas;
  }
  private float cur_x, cur_y;
  @Override public boolean onTouchEvent(MotionEvent event) {
   float x = event.getX();
   float y = event.getY();
   switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
     cur_x = x;
     cur_y = y;
     path.moveTo(cur_x, cur_y);
     break;
    }
    case MotionEvent.ACTION_MOVE: {
     path.quadTo(cur_x, cur_y, x, y);
     cur_x = x;
     cur_y = y;
     break;
    }
    case MotionEvent.ACTION_UP: {
     cacheCanvas.drawPath(path, paint);
     path.reset();
     break;
    }
   }
   invalidate();
   return true;
  }
 }
}

布局文件:

<?xml version="1.0"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
 <ImageView
   android:id="@+id/iv_sign"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_gravity="center"
   android:layout_marginBottom="3dp"
   android:layout_weight="1"
   android:background="@color/white"
   />
 <FrameLayout
   android:id="@+id/fl_view"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:background="@color/white"
   />
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@android:drawable/bottom_bar"
   android:paddingTop="3dp"
   >
  <Button
    android:id="@+id/btn_ok"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="确定"
    />
  <Button
    android:id="@+id/btn_clear"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="清除"
    />
 </LinearLayout>
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • android贝塞尔曲线实现波浪效果

    android贝塞尔曲线实现波浪效果

    这篇文章主要为大家详细介绍了android贝塞尔曲线实现波浪效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Flutter开发实现底部留言板

    Flutter开发实现底部留言板

    这篇文章主要为大家详细介绍了Flutter开发实现底部留言板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android仿新浪微博个人信息界面及其他效果

    Android仿新浪微博个人信息界面及其他效果

    这篇文章主要为大家详细介绍了Android仿新浪微博个人信息界面及其他效果设计,如正则表达式如何匹配相应表情字段,处理微博发出时间距现在时刻的时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android ListView和Adapter数据适配器的简单介绍

    Android ListView和Adapter数据适配器的简单介绍

    这篇文章主要介绍了Android ListView和Adapter数据适配器的简单介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Android安卓中循环录像并检测内存卡容量

    Android安卓中循环录像并检测内存卡容量

    这篇文章主要介绍了Android安卓中循环录像并检测内存卡容量,当内存卡空间已满时,本文还实现自动删除视频列表里面的第一个文件,需要的朋友可以参考下
    2015-06-06
  • Android Studio kotlin生成编辑类注释代码

    Android Studio kotlin生成编辑类注释代码

    这篇文章主要介绍了Android Studio kotlin生成编辑类注释代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android Studio3.0新特性及安装图文教程

    Android Studio3.0新特性及安装图文教程

    这篇文章主要为大家详细介绍了Android Studio3.0安装配置方法图文教程,以及Android Studio3.0新特性,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android显式Intent与隐式Intent的使用详解

    Android显式Intent与隐式Intent的使用详解

    Intent的中文意思是“意图,意向”, Intent对Android的核心和灵魂,是各组件之间的桥梁。四大组件分别为Activity 、Service、BroadcastReceiver、ContentProvider。而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用
    2022-09-09
  • Android实现倒计时的按钮效果

    Android实现倒计时的按钮效果

    这篇文章主要为大家详细介绍了Android实现倒计时的按钮效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • 如何使用Flutter实现58同城中的加载动画详解

    如何使用Flutter实现58同城中的加载动画详解

    这篇文章主要给大家介绍了关于如何使用Flutter实现58同城中加载动画详的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-10-10

最新评论