Android Canvas Paint签名应用程序

时间:2012-05-23 14:52:05

标签: android canvas bitmap paint

我正在研究android绘图签名应用程序,它可以灵活地清除绘画并将画布保存为位图图像。我在这个应用程序中遇到一个问题,每当我想将画布保存为位图时,我需要检查签名是否足够长。 如果签名太短,我必须要求用户再次签名。以下是我的代码: 这有两个类。

FingerPaintActivity

public class FingerPaintActivity extends Activity implements OnClickListener{

private Button clearBtn, saveBtn;

private View myView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myView = findViewById(R.id.myView);

    clearBtn = (Button) findViewById(R.id.clearBtn);
    clearBtn.setOnClickListener(this);

    saveBtn = (Button) findViewById(R.id.submitBtn);
    saveBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    if (v == clearBtn) {

        MyView.clearCanvas();
        myView.invalidate();
        myView.refreshDrawableState();

    } else {

        myView.setDrawingCacheEnabled(true);

        myView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        myView.layout(0, 0, myView.getWidth(), myView.getHeight());
        myView.buildDrawingCache(true);
        Bitmap bm = Bitmap.createBitmap(myView.getDrawingCache());
        myView.setDrawingCacheEnabled(false);
        if (bm != null) {
            try {
                String path = Environment.getExternalStorageDirectory().toString();
                OutputStream fOut = null;
                File file = new File(path, "screentest.jpg");
                fOut = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();
                Log.e("ImagePath", "Image Path : " + MediaStore.Images.Media.insertImage( getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()));

                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
}

这是第二堂课: MyView的

public class MyView extends View {

// private static final float MINP = 0.25f;
// private static final float MAXP = 0.75f;

private Bitmap mBitmap;
private static Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public MyView(Context c) {
    super(c);

}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);
    mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@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(0xFFAAAAAA);

    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 static void clearCanvas() {
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    mCanvas.drawPaint(paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC));

}

}

我面临的唯一问题是在保存签名时我必须在保存之前检查位图或画布或绘画的大小。我不知道要检查哪一个 如果有人有想法,请帮助我。

1 个答案:

答案 0 :(得分:1)

一种简单的方法是总结触摸坐标之间的距离,如果总和大于某个阈值,则只接受签名。

因此,保持touch_move()

中绘制的总距离的运行总和
mTotal += Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));

然后在

if(mTotal >= MINIMUM_SIGNATURE_LENGTH)
{
    acceptSignature();
}