0

I've created a class which extends the ImageView allowing me to draw rectangles onto the ImageView on touch.

I can draw rectangles however they only display when I'm dragging the rectangle from the starting point downward and to the right.

If I drag the rectangle from the starting point upwards or to the left then no rectangle is drawn. OnDraw() fires but nothing displays.

My class:

public class TagImageView extends ImageView {

    private float downX;
    private float downY;
    private float upX;
    private float upY;
    private Paint paint;

    public TagImageView(Context context) {
        super(context);   
        init();
    }

    public TagImageView(Context context, AttributeSet attrs) {
        super(context, attrs);     
        init();
    }

    public TagImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:   
            downX = event.getX();
            downY = event.getY();
            invalidate();            

            return true;
        case MotionEvent.ACTION_MOVE:
            upX = event.getX();
            upY = event.getY();
            invalidate();

            return true;
        case MotionEvent.ACTION_UP:
            invalidate();

            return true;
        case MotionEvent.ACTION_CANCEL:
            break;
        default:
            break;
        }      

        return super.onTouchEvent(event);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(downX, downY, upX, upY, paint);
    }
}

Edit: Not sure if this is relevant? https://stackoverflow.com/a/24168545/2380071

Community
  • 1
  • 1
LKB
  • 1,020
  • 5
  • 22
  • 46
  • can you debug and check wat values are present in `downX` `downY` `upX` `upY` during `onDraw`. Or simple write `Log.d` inside `onDraw` method and check the values – Panther Oct 02 '15 at 09:19
  • Yeah they seem valid, in a case where nothing was drawn: 591, 1225, 274, 965. In a case where the rectangle is drawn, downX < upX and downY < upY. – LKB Oct 02 '15 at 09:21
  • The `downX` and `downY` is the `startX` and `startY` for the rectangle. I believe this should be always greater than the `endX` and `endY` which in your case is `upX` and `upY` Try to pass `canvas.drawRect(upX, upY,downX, downY, paint);` if `upX` and `upY` is `>` than `downX` and `downY`. Hope this works – Panther Oct 02 '15 at 09:41
  • Thanks @Panther for your advice. If I implemented your suggestion then I believe I would encounter the same problem if I were to draw the rectangle from the starting point downward and to the right (meaning upX > downX or upY > downY). I believe I have to put in checks to see if downX and downY are less than or greater than upX and upY then handle accordingly. – LKB Oct 02 '15 at 09:45

1 Answers1

0

Well, I just ended up modifying my OnDraw() method to check the values of the downX, downY and upX, upY points. Trivial, really... although not sure if it's the most efficient. Welcome to any suggestions (or, er.. pointers)!

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (downX < upX && downY < upY)
        canvas.drawRect(downX, downY, upX, upY, paint);
    else if (downX > upX && downY < upY)
        canvas.drawRect(upX, downY, downX, upY, paint);
    else if (downX < upX && downY > upY)
        canvas.drawRect(downX, upY, upX, downY, paint);
    else
        canvas.drawRect(upX, upY, downX, downY, paint);
}
LKB
  • 1,020
  • 5
  • 22
  • 46