Android仿银行客户签名并且保存签名的截图文件并命名为本地时间
作者:安卓小飞 发布时间:2023-09-19 22:42:48
标签: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仿银行客户签名并且保存签名的截图文件并命名为本地时间网站的支持!
来源:http://blog.csdn.net/qq_33847549/article/details/75045125


猜你喜欢
- 无意中在一个国外的站点下到了一个利用WCF实现聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了测试和部分修改,感觉还
- 为什么使用Swagger 在实际开发中我们作为后端总是给前端或者其他系统提供接口,每次写完代码之后不可避
- 序列化和反序列化Java是面向对象的语言,与其他语言进行交互(比如与前端js进行http通信),需要把对象转化成一种通用的格式比如json(
- 以前一直看见 i18N ,现在才知道原来 i18N 就是 Internationalization,因为以 i 开头,以 N 结尾,共18个
- Dataway介绍Dataway 是基于 DataQL 服务聚合能力,为应用提供的一个接口配置工具。使得使用者无需开发任何代码就配置一个满足
- 在项目中如果涉及到用Excel开发的报表模版来导出报表数据的话,一般都是在Excel报表中使用VBA做成宏来进行调用。即先使用Excel自带
- 利用Jconsole工具查看程序的资源占用请求。安装jdk时bin目录有jconsole.exe工具,或者通过 Win + R,输入jcon
- 目录常见的实现方式效果图:完整代码布局代码常见的实现方式切图,做旋转动画自定义View,绘制效果gif图1、切图会增加体积,但相对简单,不过
- 1、直接使用getWindow().getDecorView().getRootView()直接使用getWindow().getDecor
- 一、idea打包项目第一步点击右边maven第二步点击compile,编译代码,编译成功后(双击运行)第三步点击package,打包代码二、
- 内部类1. 内部类简介(1) 内部类提供了更好的封装,可以把内部类隐藏在外部类之内,不允许同一个包中的其他类访问该类。(2) 内部类成员可以
- 本文实例为大家分享了Java实现猜数字游戏的具体代码,供大家参考,具体内容如下完成猜数字游戏需要实现以下几点:获得一个随机数作为“答案数”;
- 今天导入了一个模型,但是模型贴图丢失了,而且Inspector面板中处于不能编辑的状态虽然可以通过重新创建材质来替换,但是这样会生成一个新的
- 今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端。 <?xml version
- 我们经常用简单数据类型,比如int作为泛型Dictionary<TKey,TValue>的key,但有时候我们希望自定义数据类型
- 发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客。本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有
- 在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,由于项目使用了Spring+MyBatis的配置,所以打算使用M
- 程序生成的自定义文件,比如后缀是.test这种文件怎么直接启动打开程序,并打开本文件呢 1、
- @RequestBody部分属性丢失问题描述JavaBean实现public class VerifyNewFriendApplyReq i
- 代码如下一、创建CheckCode.xaml代码如下<ResourceDictionary xmlns="http