1

How to draw a line using ontouchevent in image Bitmap as a background in android.here i am using image Bitmap as a background image in android.But the image will not be Overlapped if i draw a line.

2 Answers2

2

this is a beautiful link, u can follow this for your solution:

How to draw a line in android

Community
  • 1
  • 1
jyotiprakash
  • 2,066
  • 1
  • 19
  • 25
0

use something like this in your on touch

  /////declare variable/////
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
Canvas canvas;
Paint paint;

 ///////put this in on create//////

    imageView = (ImageView)findViewById(R.id.ImageView);

    bitmap = Bitmap.createBitmap(480,640,Bitmap.Config.ARGB_8888);

Display currentDisplay = getWindowManager().getDefaultDisplay();
            float dw = currentDisplay.getWidth();
            float dh = currentDisplay.getHeight();


            canvas = new Canvas(bitmap);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setStrokeWidth(6);
            paint.setStrokeMiter(2);
            paint.setStyle(Paint.Style.STROKE); 
            paint.setColor(Color.RED);
            imageView.setImageBitmap(bitmap);
            imageView.setOnTouchListener(this);
 /////bitmap = your bitmap you want to load as back ground/////
 /////your on touch method///////

    public boolean onTouch(View v, MotionEvent event) {


    int action = event.getAction();
    switch (action)
    {
    case MotionEvent.ACTION_DOWN:
    downx = event.getX();
    downy = event.getY();
    break;
    case MotionEvent.ACTION_MOVE:
        upx = event.getX();
        upy = event.getY();

        canvas.drawLine(downx, downy, upx, upy, paint);
        imageView.invalidate();
        downx = upx;
        downy = upy;
        break;
   case MotionEvent.ACTION_UP:
    upx = event.getX();
    upy = event.getY();

    canvas.drawLine(downx, downy, upx, upy, paint);
    imageView.invalidate();
   break;
    case MotionEvent.ACTION_CANCEL:
    break;
    default:
    break;
    }

    return true;


}

i hope you got your answer..

Deepak Swami
  • 3,728
  • 1
  • 27
  • 46
  • i got one errror inside the ontouch method.paint cannot be resolved –  Feb 24 '12 at 05:35
  • finally another one error in onTouchevent imageview cannot be resolved.what is the mistake –  Feb 24 '12 at 05:52
  • ya i put already.but still the imageview cannot be resolved in the line imageView.invalidate(); in ontouchevent –  Feb 24 '12 at 05:57
  • Actually in my xml file i am using the png image –  Feb 24 '12 at 06:05