Android仿银行客户签名并且保存签名的截图文件并命名为本地时间

 更新时间:2017年07月13日 11:29:27   作者:安卓小飞  
本文通过实例代码给大家介绍了Android仿银行客户签名并且保存签名的截图文件并命名为本地时间,需要的朋友可以参考下

首先需要一个自定义view用来签字使用,可以修改颜色和画笔的粗细,可以擦拭重新画

package com.android.tcm.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class SignView extends View {
  private Paint paint;
  private Canvas cacheCanvas;
  private Bitmap cachebBitmap;
  private Path path;
  static final int BACKGROUND_COLOR = Color.WHITE;
  static final int BRUSH_COLOR = Color.BLACK;
  public SignView(Context context, AttributeSet attrs, int defStyleAttr) {
   super(context, attrs, defStyleAttr);
   // initView(context);
   // TODO Auto-generated constructor stub
  }
  public SignView(Context context, AttributeSet attrs) {
   super(context, attrs);
   // initView(context);
   // TODO Auto-generated constructor stub
  }
  public SignView(Context context) {
   super(context);
   // initView(context);
   // TODO Auto-generated constructor stub
  }
  public void initView(Context context) {
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   // TODO Auto-generated method stub
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   paint = new Paint();
   paint.setAntiAlias(true);
   paint.setStrokeWidth(3);
   paint.setStyle(Paint.Style.STROKE);
   paint.setColor(Color.RED);
   path = new Path();
   cachebBitmap = Bitmap.createBitmap(
      MeasureSpec.getSize(widthMeasureSpec),
      MeasureSpec.getSize(heightMeasureSpec), Config.ARGB_8888);
   cacheCanvas = new Canvas(cachebBitmap);
   cacheCanvas.drawColor(Color.TRANSPARENT);
  }
  public Bitmap getCachebBitmap() {
   return cachebBitmap;
  }
  public void clear() {
   if (cacheCanvas != null) {
     paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
     cacheCanvas.drawPaint(paint); 
     paint = new Paint();
     paint.setAntiAlias(true);
     paint.setStrokeWidth(3);
     paint.setStyle(Paint.Style.STROKE);
     paint.setColor(Color.RED);
     invalidate();
   }
  }
  @Override
  protected void onDraw(Canvas canvas) {
   // canvas.drawColor(BRUSH_COLOR);
   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,
      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: {
     if(isListener!=null){
      isListener.sign();
     }
     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;
  }
  public interface isSignListener{
   void sign();
  }
  isSignListener isListener;
  public void setIsListener(isSignListener isListener) {
   this.isListener = isListener;
  }
}

布局代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <RelativeLayout
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginBottom="40dp">
      <com.android.tcm.view.SignView
        android:id="@+id/signView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </FrameLayout>
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:orientation="horizontal">
      <TextView
        android:id="@+id/tv_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text="擦除重签"
        android:textSize="18sp" />
      <TextView
        android:id="@+id/tv_commit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:gravity="center"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text="确认"
        android:textSize="18sp" />
    </LinearLayout>
  </RelativeLayout>
</RelativeLayout>

主函数代码,用于获取截图(id:rl)的,并且把文件保存到本地(文件夹TVC下文件命名为当前时间如20170713 10:31:31.jpg)

package com.android.tcm.activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.tcm.R;
import com.android.tcm.view.SignView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by sf.
 */
public class SignActivity extends Activity {
  private RelativeLayout rl;
  private SignView mView;
  private TextView commit, clear;
  private Bitmap mSignBitmap;
  private String signPath;
  private long time;
  private String fileName;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign);
    initView();
  }
  public void initView() {
    mView = (SignView) findViewById(R.id.signView);
    commit = (TextView) findViewById(R.id.tv_commit);
    clear = (TextView) findViewById(R.id.tv_clear);
    rl= (RelativeLayout) findViewById(R.id.rl);
    commit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
//        commit.getDrawingCache();//获取控件的截图
//        saveSign(BitmapUtil.myShot(SignActivity.this));
        rl.setDrawingCacheEnabled(true);
        saveSign(rl.getDrawingCache());
        rl.setDrawingCacheEnabled(false);
      }
    });
    clear.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        mView.clear();
      }
    });
  }
  /**
   * signPath是图片保存路径
   *
   * @param bit
   */
  public void saveSign(Bitmap bit) {
    time = System.currentTimeMillis();
    fileName = getDateTimeFromMillisecond(time);
    mSignBitmap = bit;
    signPath = createFile();
  }
  /**
   * @return
   */
  private String createFile() {
    ByteArrayOutputStream baos = null;
    String _path = null;
    try {
      String sign_dir = Environment.getExternalStorageDirectory()
          .getPath() + "/" + "TCM" + "/";
      File dir = new File(sign_dir);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      _path = sign_dir + fileName + ".jpg";
      baos = new ByteArrayOutputStream();
      mSignBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
      byte[] photoBytes = baos.toByteArray();
      if (photoBytes != null) {
        new FileOutputStream(new File(_path)).write(photoBytes);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (baos != null)
          baos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return _path;
  }
  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (mSignBitmap != null) {
      mSignBitmap.recycle();
    }
  }
  /**
   * 将毫秒转化成固定格式的时间
   * 时间格式: yyyy-MM-dd HH:mm:ss
   *
   * @param millisecond
   * @return
   */
  public static String getDateTimeFromMillisecond(Long millisecond) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(millisecond);
    String dateStr = simpleDateFormat.format(date);
    return dateStr;
  }
}

以上所述是小编给大家介绍的Android仿银行客户签名并且保存签名的截图文件并命名为本地时间,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android如何为按键添加声音

    Android如何为按键添加声音

    这篇文章主要告诉大家Android为按键添加声音的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 关于Android中Gradle和jar包下载慢的问题及解决方法

    关于Android中Gradle和jar包下载慢的问题及解决方法

    这篇文章主要介绍了解决Android中Gradle和jar包下载慢的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Android Jetpack库剖析之LiveData组件篇

    Android Jetpack库剖析之LiveData组件篇

    LiveData是Jetpack组件的一部分,更多的时候是搭配ViewModel来使用,相对于Observable,LiveData的最大优势是其具有生命感知的,换句话说,LiveData可以保证只有在组件( Activity、Fragment、Service)处于活动生命周期状态的时候才会更新数据
    2022-07-07
  • Android ConstraintLayout约束布局使用详解

    Android ConstraintLayout约束布局使用详解

    ConstraintLayout 即约束布局,也是 Android Studio 的默认布局,它可以减少布局的层级,改善布局性能。不夸张地说,它基本上可以实现任何你想要的布局效果,下面,咱们一起来瞧瞧吧
    2022-11-11
  • Android中打电话的数据流程分析

    Android中打电话的数据流程分析

    所有流程的起点是从拨号后按下拨号键开始,接下来将对打电话的数据流程进行源码分析,感兴趣的朋友可以了解下
    2013-01-01
  • Android面向单Activity开发示例解析

    Android面向单Activity开发示例解析

    这篇文章主要为大家介绍了Android面向单Activity开发示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android仿考拉全局滑动返回及联动效果的实现方法

    Android仿考拉全局滑动返回及联动效果的实现方法

    这篇文章主要给大家介绍了关于Android仿考拉全局滑动返回及联动效果的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • Android仿QQ、微信聊天界面长按提示框效果

    Android仿QQ、微信聊天界面长按提示框效果

    最近在工作项目中要实现一个长按提示 “复制” 的功能,类似于QQ、微信聊天界面长按提示框效果,本来想偷懒在网上找个开源的项目用,但是看了好几个都不是很满意,所以就打算按照自己的思路来实现一个。下面分享给大家,有需要的朋友们可以参考借鉴。
    2016-11-11
  • Android 实现定时任务的过程详解

    Android 实现定时任务的过程详解

    这篇文章主要介绍了Android 定时任务过程详解的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Android App使用RecyclerView实现上拉和下拉刷新的方法

    Android App使用RecyclerView实现上拉和下拉刷新的方法

    RecyclerView一经推出便被认为是替代ListView的存在,那么ListView的上拉和下拉刷新我们同样可以使用RecyclerView来做到,这里我们就来看一下Android App使用RecyclerView实现上拉和下拉刷新的方法,首先先来点RecyclerView的小介绍:
    2016-06-06

最新评论