SoFunction
Updated on 2025-04-07

Android imitates bank customer signature and saves the signed screenshot file and name it local time

First, a custom view is needed for signature use. You can modify the color and thickness of the brush, and you can wipe it and re-draw it.

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class SignView extends View {
  private Paint paint;
  private Canvas cacheCanvas;
  private Bitmap cachebBitmap;
  private Path path;
  static final int BACKGROUND_COLOR = ;
  static final int BRUSH_COLOR = ;
  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
   (widthMeasureSpec, heightMeasureSpec);
   paint = new Paint();
   (true);
   (3);
   ();
   ();
   path = new Path();
   cachebBitmap = (
      (widthMeasureSpec),
      (heightMeasureSpec), Config.ARGB_8888);
   cacheCanvas = new Canvas(cachebBitmap);
   ();
  }
  public Bitmap getCachebBitmap() {
   return cachebBitmap;
  }
  public void clear() {
   if (cacheCanvas != null) {
     (new PorterDuffXfermode());
     (paint); 
     paint = new Paint();
     (true);
     (3);
     ();
     ();
     invalidate();
   }
  }
  @Override
  protected void onDraw(Canvas canvas) {
   // (BRUSH_COLOR);
   (cachebBitmap, 0, 0, null);
   (path, paint);
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   int curW = cachebBitmap != null ? () : 0;
   int curH = cachebBitmap != null ? () : 0;
   if (curW >= w && curH >= h) {
     return;
   }
   if (curW < w)
     curW = w;
   if (curH < h)
     curH = h;
   Bitmap newBitmap = (curW, curH,
      Config.ARGB_8888);
   Canvas newCanvas = new Canvas();
   (newBitmap);
   if (cachebBitmap != null) {
     (cachebBitmap, 0, 0, null);
   }
   cachebBitmap = newBitmap;
   cacheCanvas = newCanvas;
  }
  private float cur_x, cur_y;
  @Override
  public boolean onTouchEvent(MotionEvent event) {
   float x = ();
   float y = ();
   switch (()) {
   case MotionEvent.ACTION_DOWN: {
     if(isListener!=null){
      ();
     }
     cur_x = x;
     cur_y = y;
     (cur_x, cur_y);
     break;
   }
   case MotionEvent.ACTION_MOVE: {
     (cur_x, cur_y, x, y);
     cur_x = x;
     cur_y = y;
     break;
   }
   case MotionEvent.ACTION_UP: {
     (path, paint);
     ();
     break;
   }
   }
   invalidate();
   return true;
  }
  public interface isSignListener{
   void sign();
  }
  isSignListener isListener;
  public void setIsListener(isSignListener isListener) {
    = isListener;
  }
}

The layout code is as follows

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  &lt;RelativeLayout
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"&gt;
    &lt;FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginBottom="40dp"&gt;
      &lt;
        android:
        android:layout_width="match_parent"
        android:layout_height="match_parent" /&gt;
    &lt;/FrameLayout&gt;
    &lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:orientation="horizontal"&gt;
      &lt;TextView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text="Erase Re-sign"
        android:textSize="18sp" /&gt;
      &lt;TextView
        android:
        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="confirm"
        android:textSize="18sp" /&gt;
    &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;

The main function code is used to obtain screenshots (id:rl) and save the file locally (the file under the folder TVC is named as the current time, as 20170713 10:31:)

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
 * 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) {
    (savedInstanceState);
    setContentView(.activity_sign);
    initView();
  }
  public void initView() {
    mView = (SignView) findViewById();
    commit = (TextView) findViewById(.tv_commit);
    clear = (TextView) findViewById(.tv_clear);
    rl= (RelativeLayout) findViewById();
    (new () {
      @Override
      public void onClick(View arg0) {
// ();// Get screenshot of the control//        saveSign(());
        (true);
        saveSign(());
        (false);
      }
    });
    (new () {
      @Override
      public void onClick(View arg0) {
        ();
      }
    });
  }
  /**
    * signPath is the image saving path
    *
    * @param bit
    */
  public void saveSign(Bitmap bit) {
    time = ();
    fileName = getDateTimeFromMillisecond(time);
    mSignBitmap = bit;
    signPath = createFile();
  }
  /**
   * @return
   */
  private String createFile() {
    ByteArrayOutputStream baos = null;
    String _path = null;
    try {
      String sign_dir = ()
          .getPath() + "/" + "TCM" + "/";
      File dir = new File(sign_dir);
      if (!()) {
        ();
      }
      _path = sign_dir + fileName + ".jpg";
      baos = new ByteArrayOutputStream();
      (, 100, baos);
      byte[] photoBytes = ();
      if (photoBytes != null) {
        new FileOutputStream(new File(_path)).write(photoBytes);
      }
    } catch (IOException e) {
      ();
    } finally {
      try {
        if (baos != null)
          ();
      } catch (IOException e) {
        ();
      }
    }
    return _path;
  }
  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    ();
    if (mSignBitmap != null) {
      ();
    }
  }
  /**
    * Time to convert milliseconds into fixed format
    * Time format: yyyy-MM-dd HH:mm:ss
    *
    * @param millionsecond
    * @return
    */
  public static String getDateTimeFromMillisecond(Long millisecond) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(millisecond);
    String dateStr = (date);
    return dateStr;
  }
}

The above is the screenshot file of Android imitation bank customer signatures and saves signatures and named them local time. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!