在Android上使用drawRect和onTouch在一个视图中绘制多个矩形

时间:2016-01-28 06:55:52

标签: android eclipse

我可以绘制单个矩形但单击按钮后无法绘制新矩形。请帮忙!我想为每次触摸和拖动绘制一个新的矩形。提前谢谢!

@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);
    }
}

我找不到错误在哪里......

2 个答案:

答案 0 :(得分:0)

因为你画一个矩形时,屏幕会被重新绘制。所以你应该在绘制矩形时保存矩形。

答案 1 :(得分:0)

像这样:

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

在onDraw()方法类中:

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

每次绘制矩形时,您可以先绘制列表中保存的信息的矩形,然后绘制新的矩形并将其信息保存到列表中。