Android 提供了很多丰富、实用而且很有特色的功能。比如,语音识别、手写签名等等。本篇就为你介绍如何在android上进行个性化的手写签名。

首先大致说说需求:这是一个追求时尚、张扬个性的时代,我们希望在签名的地方,签名的是自己手写出来的很有个性的艺术字,而非根据手势识别出来的标准字体。

设计思路如下,在画板上进行签名(其实就是绘制图片),完成后保存为图片。然后将图片按照一定的比率进行缩放并显示在指定的位置。

这里给出一个实例,实例只是一个简单的例子,如有需要可以进行必要的扩展。这里我们需要一个Listener、一个Dialog、一个Activity这个三个java类。两个layout XML文件。

Listener很简单,主要是对手写板对话框的一个监听。public interface DialogListener {

public void refreshActivity(Object object);

}

接着是画板的Dialogpackage cn.handwriting;

import android.app.Dialog;

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.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.Window;

import android.view.WindowManager.LayoutParams;

import android.widget.Button;

import android.widget.FrameLayout;

public class WritePadDialog extends Dialog {

Context context;

LayoutParams p ;

DialogListener dialogListener;

public WritePadDialog(Context context,DialogListener dialogListener) {

super(context);

this.context = context;

this.dialogListener = dialogListener;

}

static final int BACKGROUND_COLOR = Color.WHITE;

static final int BRUSH_COLOR = Color.BLACK;

PaintView mView;

/** The index of the current color to use. */

int mColorIndex;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

requestWindowFeature(Window.FEATURE_PROGRESS);

setContentView(R.layout.write_pad);

p = getWindow().getAttributes(); //获取对话框当前的参数值

p.height = 320;//(int) (d.getHeight() * 0.4); //高度设置为屏幕的0.4

p.width = 480;//(int) (d.getWidth() * 0.6); //宽度设置为屏幕的0.6

getWindow().setAttributes(p); //设置生效

mView = new PaintView(context);

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tablet_view);

frameLayout.addView(mView);

mView.requestFocus();

Button btnClear = (Button) findViewById(R.id.tablet_clear);

btnClear.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mView.clear();

}

});

Button btnOk = (Button) findViewById(R.id.tablet_ok);

btnOk.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try {

dialogListener.refreshActivity(mView.getCachebBitmap());

WritePadDialog.this.dismiss();

} catch (Exception e) {

e.printStackTrace();

}

}

});

Button btnCancel = (Button)findViewById(R.id.tablet_cancel);

btnCancel.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

cancel();

}

});

}

/**

* This view implements the drawing canvas.

*

* It handles all of the input events and drawing functions.

*/

class PaintView extends View {

private Paint paint;

private Canvas cacheCanvas;

private Bitmap cachebBitmap;

private Path path;

public Bitmap getCachebBitmap() {

return cachebBitmap;

}

public PaintView(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(p.width, (int)(p.height*0.8), Config.ARGB_8888);

cacheCanvas = new Canvas(cachebBitmap);

cacheCanvas.drawColor(Color.WHITE);

}

public void clear() {

if (cacheCanvas != null) {

paint.setColor(BACKGROUND_COLOR);

cacheCanvas.drawPaint(paint);

paint.setColor(Color.BLACK);

cacheCanvas.drawColor(Color.WHITE);

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, 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;

}

}

}

Activity是程序的入口,这个必不可少。package cn.handwriting;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import android.app.Activity;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import android.widget.TextView;

public class HandwritingActivity extends Activity {

/** Called when the activity is first created. */

private Bitmap mSignBitmap;

private String signPath;

private ImageView ivSign;

private TextView tvSign;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setTitle("欢迎使用手写签名");

ivSign =(ImageView)findViewById(R.id.iv_sign);

tvSign = (TextView)findViewById(R.id.tv_sign);

ivSign.setOnClickListener(signListener);

tvSign.setOnClickListener(signListener);

}

private OnClickListener signListener = new View.OnClickListener() {

@Override

public void onClick(View v) {

WritePadDialog writeTabletDialog = new WritePadDialog(

HandwritingActivity.this, new DialogListener() {

@Override

public void refreshActivity(Object object) {

mSignBitmap = (Bitmap) object;

signPath = createFile();

/*BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 15;

options.inTempStorage = new byte[5 * 1024];

Bitmap zoombm = BitmapFactory.decodeFile(signPath, options);*/

ivSign.setImageBitmap(mSignBitmap);

tvSign.setVisibility(View.GONE);

}

});

writeTabletDialog.show();

}

};

/**

* 创建手写签名文件

*

* @return

*/

private String createFile() {

ByteArrayOutputStream baos = null;

String _path = null;

try {

String sign_dir = Environment.getExternalStorageDirectory() + File.separator;

_path = sign_dir + System.currentTimeMillis() + ".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;

}

}

对应的两个layout文件

main.xml<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/iv_sign"

android:layout_marginTop="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

/>

android:id="@+id/tv_sign"

android:layout_marginTop="50dp"

android:layout_below="@id/iv_sign"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="点此签名"

/>

write_pad.xml

xmlns:greendroid="http://schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/tablet_view"

android:layout_width="fill_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="@color/white">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/bottom_bar"

android:paddingTop="4dp" >

android:id="@+id/tablet_ok"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="确定" />

android:id="@+id/tablet_clear"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="清除" />

android:id="@+id/tablet_cancel"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="取消" />

这里还有个样式的设置,所以在values下添加了一个colors.xml文件。

android手写汉字,Android 手写输入的实现(保存涂鸦文字)相关推荐

  1. android实现对PDF进行签名、涂鸦操作

    在android项目开发过程中,项目需要android客户端完成对PDF文件的涂鸦.签名操作,其中最重要的就是手写签名了,可以当做是凭证一类的东西.在网上只能找到一个例子,然而做的不是很完善(地址下面 ...

  2. Android App内截屏监控及涂鸦功能实现

    Android App内截屏监控及涂鸦功能实现 Android截屏功能是一个常用的功能,可以方便的用来分享或者发送给好友,本文介绍了如何实现app内截屏监控功能,当发现用户在我们的app内进行了截屏操 ...

  3. Android心得4.1--文件的保存与读取及文件的操作模式详解.doc

    一.保存到手机内存 1.  很多时候我们的软件需要对处理后的数据进行存储或再次访问.Android为数据存储提供了多种方式,分别有如下几种: l     文件(采用IO数据流的方式) l     Sh ...

  4. android view存储为jpg,Android长按imageview把图片保存到本地的实例代码

    工具类 之前用 AsyncTask 现在改用rxJava public class SaveImageUtils { public static void imageSave(final ImageV ...

  5. android java 实体类 object变量 保存_Android中Intent传递对象的两种方法Serializable,Parcelable...

    Android中的传递有两个方法,一个是Serializable,另一个是Parcelable. Serializable是J2SE本身就支持的.而Parcelable是Android所特有的. 二者 ...

  6. android系统相机自动录像,android 调用系统相机录像并保存

    1.在AndroidManifest.xml中添加如下代码 tools:ignore="ProtectedPermissions" /> android:authoritie ...

  7. 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式

    上一篇跟各位童鞋介绍了SharedPreference 和 File流如何存储数据,并且推荐使用FileOutputStream/FileInputStream来存储咱们游戏数据,那么这一篇则是像大家 ...

  8. Android学习小记-----监听并保存传感器数据,让service后台运行(保持CPU运转

    最近做了一个Demo,监听手机中传感器的数据,并将数据保存到手机文件中,发现数据会有丢失的现象. 经过多次测试,发现系统进入深度休眠了,之后service会停止,虽然增加了service自动启动的功能 ...

  9. Android doc|Getting Started|部分 --转载 保存数据

    保存数据 大多数 Android 应用需要保存数据,即使仅保存在 onPause() 过程中与应用状态有关的信息,以便用户进度不会丢失 . 大多数非平凡应用也需要保存用户设置,并且有些应用必须在文件和 ...

最新文章

  1. 万字长文总结机器学习的模型评估与调参
  2. vscode中设置字体大小_vscode配置使用教程
  3. 重温java中的String,StringBuffer,StringBuilder类
  4. Leetcode62 DP
  5. Java 源码中 unchecked 什么意思
  6. ubuntu安装python3.6_如何在Ubuntu19.04上安装Python3.6?
  7. DM368 Uboot
  8. WeihanLi.Npoi 1.18.0 Released
  9. qr码生成_从Java程序生成QR码图像
  10. sessionStorage什么时候失效
  11. 滑雪(信息学奥赛一本通-T1280)
  12. python列表相加的方法:两个list [] 加法
  13. LayaAir UI 组件 # Image 位图、Label 标签
  14. php_curl模拟登录有验证码实例
  15. stm32无源蜂鸣器定时器_【STM32H7教程】第20章 STM32H7的GPIO应用之无源蜂鸣器...
  16. ads2020卸载 ads软件怎么卸载干净ads2016 ads2019卸载不干净无法重新安装 ads2017彻底卸载 ads2017卸载时删不尽
  17. idea常用22种快捷键,脱离鼠标,便捷开发,赶紧收藏
  18. 本地图文直接复制到动易CMS编辑器中
  19. 18、弱电锦集:网络视频监控知识问答100例
  20. 前端容易忽略的 debugger 调试技巧

热门文章

  1. spring boot配置阿里云maven库
  2. 置换检验(permutation tests)是什么?置换检验解决什么问题?置换检验的基本步骤示例、Bootstrapping是什么?自助法计算置信区间步骤示例
  3. 宽带用户可以免QQ技术费开通QQ会员和黄钻
  4. 路由器端WAN口和LAN端口
  5. can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM
  6. 为什么MySQL端口号为0_mysql查看端口为0
  7. 海康威视人脸门禁对接开发(一)调用设备篇
  8. UltraRAM:在UltraScale+器件上集成嵌入式存储器
  9. 数据库编程入门(一)-PL/SQL快速入门
  10. THREEJS实现标签,自定义样式显示标签,在场景图上动态标识一些文字方法一(css2d_label)