欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-08-08 11:07:43
首先需要一个自定义view用来签字使用,可以修改颜色和画笔的粗细,可以擦拭重新画 package com.android.tcm.view; import an...

首先需要一个自定义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仿银行客户签名并且保存签名的截图文件并命名为本地时间,希望对大家有所帮助