English 中文(简体)
和机器人油罐涂画签名应用程序
原标题:Android Canvas Paint Signature Application

I am working on android paint signature application where it has the flexibility to clear the paint and save the canvas as bitmap image. I am facing an issue in this app, whenever i want to save the canvas as bitmap i need to check whether the signature is lengthy enough or not. If the signature is too short them i have to ask the user to put a signature again. The following is my code: There are two classes in this.

FeperPaint 活性

public class FeperPaint 活性 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();
            时 时
        时 时
    时 时
时 时
时 时

This is the second class: 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));

时 时

时 时

The only problem i am facing is while saving the signature i have to check the size of bitmap or canvas or paint before saving it. i don t know which one to check for If anyone has idea please help me out.

最佳回答

简单的做法是总结触摸坐标之间的距离,只有在总和大于某一阈值时才接受签字。

所以, 在触摸_ 移动中保持总距离的运行总和 ()

mTotal += Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));

之后

if(mTotal >= MINIMUM_SIGNATURE_LENGTH)
{
    acceptSignature();
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签