Draw Multiple Rectangles in One View using drawRect and onTouch on Android

I can draw a single rectangle but cannot draw a new one once a button is clicked. Please help! I want to draw a new rectangle for every touch and drag. Thanks in advance!

@Override
protected void onDraw(Canvas canvas) {
    if(points[3]==null) //point4 null when user did not touch and move on screen.
        return;
    int left, top, right, bottom;
    left = points[0].x;
    top = points[0].y;
    right = points[0].x;
    bottom = points[0].y;
    for (int i = 1; i < points.length; i++) {
        left = left > points[i].x ? points[i].x:left;
        top = top > points[i].y ? points[i].y:top;
        right = right < points[i].x ? points[i].x:right;
        bottom = bottom < points[i].y ? points[i].y:bottom;
    }
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(10);

    //draw stroke
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.parseColor("#AADB1255"));
    paint.setStrokeWidth(10);
    canvas.drawRect(
                left + colorballs.get(0).getWidthOfBall() / 2,
                top + colorballs.get(0).getWidthOfBall() / 2, 
                right + colorballs.get(2).getWidthOfBall() / 2, 
                bottom + colorballs.get(2).getWidthOfBall() / 2, paint);

    //fill the rectangle
    paint.setStyle(paint.Style.FILL);
    paint.setColor(Color.parseColor("#55DB1255"));
    paint.setStrokeWidth(0);
    canvas.drawRect(
            left + colorballs.get(0).getWidthOfBall() / 2,
            top + colorballs.get(0).getWidthOfBall() / 2, 
            right + colorballs.get(2).getWidthOfBall() / 2, 
            bottom + colorballs.get(2).getWidthOfBall() / 2, paint);

    //draw the corners
    BitmapDrawable bitmap = new BitmapDrawable();
     //draw the balls on the canvcasPaint
    paint.setColor(Color.BLUE);
    paint.setTextSize(22);
    paint.setStrokeWidth(0);
    for (int i =0; i < colorballs.size(); i ++) {
        ColorBall ball = colorballs.get(i);

        switch(i) {
        case 0:
            ax = ball.getX();
            ay = ball.getY();
            break;
        case 1:
            bx = ball.getX();
            by = ball.getY();

            width = Math.round(Math.sqrt((double) ((bx - ax) * (bx - ax)) + ((by - ay) * (by - ay))) * 2.54 / 96);
            break;
        case 2:
            cx = ball.getX();
            cy = ball.getY();

            length = Math.round(Math.sqrt((double) ((cx - bx) * (cx - bx)) + ((by - cy) * (by - cy))) * 2.54 / 96);
            break;
        default:
            break;
        }

        area = length * width;
        canvas.drawText("length = " + length + "cm", (cx + bx) / 2, (by + cy) / 2, paint);
        canvas.drawText("width = " + width + "cm", (bx + ax) / 2, (by + ay) / 2, paint);
        canvas.drawText("area = " + area + "cm", (cx + ax) / 2, (cy + ay) / 2, paint);
    }
}

I can't find where is the error here...

Answers


Like this:

public class RegInfo{
    int x;
    int y;
    int width;
    int height;
}

In the onDraw() method class:

ArrayList<RegInfo> lists = new ArrayLists<RegInfo>();

Every Time you draw a rectangle,You can first draw the rectange which info saved in the lists and the draw the new rectange and save its info to the lists.


Because ervery time you draw a rectangle,The screen will be repainted.So you should save the rectangle ervery time you draw the rectangle.


Need Your Help

Change <div> to <a> and add attributes/class using Jquery

jquery attr replacewith

I have an example of something I trying to achieve but not having success.

Register file extensions / mime types in Linux

linux installation mime file-type

I'm developing a Linux application that has its own file format. I want my app to open when you double-click on those files.