最近在做一个银行卡交易的项目,里面涉及到银行卡支付签名的问题,所以自定义了一个画板,用来输入顾客签名.
下面是一个自定义DrawView用来接收顾客的输入:
package com.cnziz.printtest.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.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
private static final String TA_STRING = DrawView.class.getSimpleName();
private Paint mOutterPaint;
private Path mPath;
private Canvas mCanvas;
private Bitmap mBitmap;
private int mLastX, mlastY;
private int bitmapWidth;
private int bitmapHeight;
public DrawView(Context context) {
this(context, null);
}
public DrawView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
Log.i(TA_STRING, "new");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
bitmapWidth = getMeasuredWidth();
bitmapHeight = getMeasuredHeight();
// 初始化我们的bitmap
mBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
Log.i(TA_STRING, TA_STRING + " onMeasure");
}
/**
* 设置画笔
*/
private void setOutPaint() {
mOutterPaint.setColor(Color.BLACK);
mOutterPaint.setAntiAlias(true);
mOutterPaint.setDither(true);
mOutterPaint.setStrokeJoin(Paint.Join.ROUND);
mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
mOutterPaint.setStyle(Style.STROKE);
mOutterPaint.setStrokeWidth(2);
}
/**
* 进行一些初始化操作
*/
private void init() {
mOutterPaint = new Paint();
mPath = new Path();
// 设置绘制path画笔的一些属性
setOutPaint();
Log.i(TA_STRING, TA_STRING + " is init ");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
mLastX = x;
mlastY = y;
mPath.moveTo(mLastX, mlastY);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x - mLastX);
int dy = Math.abs(y - mlastY);
if (dx > 3 || dy > 3) {
mPath.lineTo(x, y);
}
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
Log.i(TA_STRING, "onDraw");
drawPath();
canvas.drawBitmap(mBitmap, 0, 0, null);
super.onDraw(canvas);
}
private void drawPath() {
mCanvas.drawPath(mPath, mOutterPaint);
}
/**
* 清除
*/
public void clear(){
mBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mOutterPaint = new Paint();
mPath = new Path();
// 设置绘制path画笔的一些属性
setOutPaint();
invalidate();
}
}
这个DrawView的用法跟其他的自定义控件一样
下面是生成Bitmap的方法
//必须要有
mDrawView.setDrawingCacheEnabled(true);
//这句代码的作用是设置获取截图的背景,如果不加这句代码,获取的截图回事黑的 mDrawView.setDrawingCacheBackgroundColor(Color.WHITE);
//将获取的截图复制一份,不然的话"mDrawView.setDrawingCacheEnabled(false);"将会把bitmap释放掉
Bitmap bm = Bitmap.createBitmap(mDrawView.getDrawingCache());
//必须要有
mDrawView.setDrawingCacheEnabled(false);
//调节bitmap的尺寸,2代表新生成的bitmap长宽都是原来的一半
Bitmap bitmap = compressBitmap(bm, 2);
/**
* 对图片进行压缩(不去除透明度)
*
* @param bitmapOrg
*/
public Bitmap compressBitmap(Bitmap bitmapOrg, int minification) {
// 加载需要操作的图片,这里是一张图片
// bitmapOrg =
// BitmapFactory.decodeResource(getResources(),R.drawable.user);
// 获取这个图片的宽和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
// 定义预转换成的图片的宽度和高度
int newWidth = width / minification;
int newHeight = height / minification;
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
// 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中
// BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
return resizedBitmap;
}
大概就是这样,主要代码都有了,具体的项目会在下面放出来