以下是我的Java代码。我想捕获位图中绘制的签名并将其存储在字符串数组中(签名的X和Y坐标)。
package com.ust.mobile.android.jnj;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
public class SignatureFieldClass extends View{
private int nBitmapWidth = 700;
private int nBitmapHeight = 220;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint = new Paint();
private ArrayList<String> signature=new ArrayList<String>();
public SignatureFieldClass(Context c) {
super(c);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
mBitmap = Bitmap.createBitmap(nBitmapWidth, nBitmapHeight, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFF6386AD);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
public void ClearPath(){
mPath.reset();
invalidate();
}
}
答案 0 :(得分:1)
我们可以将位图的X和Y坐标存储到字符串数组中。对于我们可以在代码下面使用的数据。 private int mX,mY; private static final float TOUCH_TOLERANCE = 4;
private void touch_start(int x, int y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
s=Integer.toString(mX);
//signatures=s;
sb.append(s);
sb.append(",");
//signature[0]=s;
mY = y;
s=Integer.toString(mY);
sb.append(s);
//signature[1]=s;
}
private void touch_move(int x, int y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
s=Integer.toString(mX);
sb.append(",");
sb.append(s);
sb.append(",");
/* int l=signature.length;
signature[l]=s;*/
mY = y;
s=Integer.toString(mY);
sb.append(s);
/*l=signature.length;
signature[l]=s;*/
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
s=Integer.toString(mX);
sb.append(",");
sb.append(s);
s=Integer.toString(mY);
sb.append(",");
sb.append(s);
sb.append(",");
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
这不是完整的代码。部分f代码数据捕获x和y coordinates.sb是一个字符串缓冲区。我们可以将d字符串缓冲区中的d值复制到字符串中。