-1

enter image description hereI need to draw line and edit it by resize,delete etc.,I need to resize the line with user touch event.Currently i am able to draw line

  private void onDrawLine(Canvas canvas) {

    float dx = Math.abs(mx - mStartX);
    float dy = Math.abs(my - mStartY);

    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {

        canvas.drawLine(mStartX, mStartY, mx, my, mPaint);
        /*bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        canvas.drawBitmap(bm, mStartX, mStartY, null);        
        canvas.drawBitmap(bm, mx, my, null);  */      

    }
}

private void onTouchEventLine(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            isDrawing = true;
            mStartX = mx;
            mStartY = my;
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:

            invalidate();
            break;
        case MotionEvent.ACTION_UP:

             isDrawing = false;
             mCanvas.drawLine(mStartX, mStartY, mx, my, mPaintFinal);
             invalidate();
            break;
    }
}

For Resize,i found for rectangle i need to implement for line.Here is the link for resize rectangle with user touch event

https://stackoverflow.com/a/17807469/2365507

Please help me

Community
  • 1
  • 1
Sai Aditya
  • 2,353
  • 1
  • 14
  • 16
  • what exactly do you want to achieve? "How to resize canvas line" has completely no sense... – pskink Mar 10 '15 at 07:23
  • @pskink i need to draw line or arrow between two icons and if icons will placed on some other place using drag.i need to resize the line to that new positions of icon – Sai Aditya Mar 10 '15 at 07:28
  • maybe other people will understand you since i cant get what you really mean ... – pskink Mar 10 '15 at 07:31
  • A bit late, but can be helpful to others. Derived from answer from https://stackoverflow.com/questions/8974088/how-to-create-a-resizable-rectangle-with-user-touch-events-on-android/17807469#17807469 – Anup Pokhrel Feb 06 '20 at 06:54

1 Answers1

1

Get the newX, newY points of new position, where you want line to be and drawLine again

canvas.drawLine(mStartX, mStartY, newX, newY , mPaint);

and call invalidate() to redraw on canvas

Androider
  • 2,884
  • 5
  • 28
  • 47
  • Please give some example – Sai Aditya Mar 10 '15 at 08:48
  • It is hard to find example of exactly what you want, but i can explain if you are stuck at any point – Androider Mar 10 '15 at 09:07
  • when i have to get newX and newY values?is it in ACTION_UP?I tried to get new x and y values,line gets redrawn from the endpoint.but i want to resize the old line – Sai Aditya Mar 10 '15 at 09:16
  • do u mean "animate to large size" by "resize" – Androider Mar 10 '15 at 09:20
  • you will not do it in onTouchEventLine(), but from other touchEvent method. Like the icons's touch event/drag event – Androider Mar 10 '15 at 09:30
  • How can i implement in onTouchEvent with event.getX() and event.getY()??I am unable to move this discussion to chat,will you? – Sai Aditya Mar 10 '15 at 09:35
  • please check this Nguyen Minh Binh answer,he resize the rectangle with user touch event,but i need to resize line.how can i achieve??http://stackoverflow.com/a/17807469/2365507 – Sai Aditya Mar 10 '15 at 09:57