Android自定义控件--2(数字签名)

客户信息交互用到的一个数字签名,保存的是bmp图片(我那会在PAD端保存成图片比较容易,关键是上传到PC端问题大了 用USB-HID方式传总是出错,因为每个包我发送255字节,最后一个包发错了,就在PC上保存不成完整的图片,尝试过用串口传输,但是速度较慢,容易丢包)以下是源码:
/**
 * 
 */
package com.hacheng.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hacheng.ttsdemo.R;


/**
 * @author:xj
 * @version:v1.0
 * @company:
 */
public class PaintView extends View {

	private static final float DEFAULT_BLOD_WIDTH = 4.0f;
	private static final int DEFAULT_COLOR = Color.BLACK;

	private List<Float> points = new ArrayList<Float>();

	private Paint paint; // 画笔
	private Canvas cacheCanvas;// 画布
	private Bitmap cachebBitmap;

	private Path path;// 轨迹
	private float curX;
	private float curY;

	private float blodWidth = DEFAULT_BLOD_WIDTH;
	private int color = DEFAULT_COLOR;

	public Bitmap getCachebBitmap() {
		return cachebBitmap;
	}

	public PaintView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.paintView);
		blodWidth = typedArray.getFloat(R.styleable.paintView_blodWidth,
				DEFAULT_BLOD_WIDTH);
		color = typedArray.getColor(R.styleable.paintView_color, DEFAULT_COLOR);
		init();
	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(blodWidth);
		paint.setColor(color);
		paint.setStyle(Paint.Style.STROKE);
		path = new Path();
	}

	public void clear() {
		if (cacheCanvas != null) {
			paint.setColor(Color.BLUE);
			cacheCanvas.drawPaint(paint);
			paint.setColor(Color.BLACK);
			cacheCanvas.drawColor(Color.WHITE);
			invalidate();
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (cachebBitmap == null || cacheCanvas == null) {
			cachebBitmap = Bitmap.createBitmap(this.getWidth(),
					this.getHeight(), Config.ARGB_8888);
			cacheCanvas = new Canvas(cachebBitmap);
			cacheCanvas.drawColor(Color.WHITE);
		}
		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;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		float x = event.getX();
		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			curX = x;
			curY = y;
			points.add(x);
			points.add(y);
			path.moveTo(curX, curY);
			break;

		case MotionEvent.ACTION_MOVE:
			path.quadTo(curX, curY, x, y);
			curX = x;
			curY = y;
			points.add(x);
			points.add(y);
			break;

		case MotionEvent.ACTION_UP:
			cacheCanvas.drawPath(path, paint);
			path.reset();
			break;
		default:
			break;
		}

		invalidate();

		return true;
	}

	public List<Float> getPoints() {
		return points;
	}
}

源码下载请戳这里:

你可能感兴趣的:(java,android)