2012-05-23 52 views
0

我正在處理android paint簽名應用程序,它具有清除油漆並將canvas保存爲位圖圖像的靈活性。我在這個應用程序中遇到了一個問題,每當我想將畫布保存爲位圖時,我需要檢查簽名是否足夠長。 如果簽名太短,我不得不要求用戶再次簽名。以下是我的代碼: 這裏有兩個類。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

一個簡單的方法是總結觸摸座標之間的距離,並且只有在總和大於某個閾值時才接受簽名。

所以,保持總距離)的運行總和在TOUCH_MOVE繪製(

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

再後來就

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

感謝好友!!!!這對我幫助很大。我現在可以進行簽名驗證了! – krisDrOid

+0

沒問題,祝你好運! – Josh