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

源码下载请戳这里:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值