1

I am creating an android app where in a user can learn to write alphabet. My requirements are like this: An alphabet is displayed on canvas using Paint.drawText(). User will move his finger on the alphabet to trace it. I am able to complete the above two requirements.

Now what I want is: if the user is touching his finger anywhere other than the alphabet region(path) it should vibrate/make a buzz. The Question is: How do I come to know if the user is touching away from the alphabet path/region?

Here is the code snippet.

    public class MyView extends View {

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

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

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

        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);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setTextSize(200);
        p.setColor(Color.BLACK);
        canvas.drawText("A", 300, 150, p);
       // canvas.drawLine(mX, mY, Mx1, My1, mPaint);
       // canvas.drawLine(mX, mY, x, y, mPaint);
        canvas.drawPath(mPath, mPaint);
        Path pa;
        canvas.get

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);


    }

    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;
        }
        mPath.lineTo(mX, mY);
        vibe.vibrate(10);
    }
    private void touch_up() {

        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
        vibe.cancel();
    }

    @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();
           //   Mx1=(int) event.getX();
             //  My1= (int) event.getY();
               invalidate();
                break;
        }
        return true;
    }
}
manohar
  • 21
  • 3
  • did u prepare two custom view for display Alphabets and draw alphabets. – SuReSh PaTi Oct 01 '12 at 07:43
  • 1
    I found out answer for this problem. The below link helped me to arrive at the solution. [Solution]: http://stackoverflow.com/questions/2597590/how-can-i-tell-if-a-closed-path-contains-a-given-point – manohar Oct 01 '12 at 09:09

0 Answers0