在不同方向绘制多个矩形android问题

时间:2017-04-21 13:41:08

标签: java android canvas logic rect

埃洛,善良的女士和温柔的男人。我陷入了这个问题,我有任何帮助将非常感激。我将尽我所能来解释究竟发生了什么,希望你能帮助我,所以在这里: 基本上我正在绘制一些墙并将它们发送到服务器上,我发送的有两件事,宽度和高度。宽度或高度是wallThickness取决于墙的方向,如果它是垂直宽度= wallThickness,如果水平高度= wallThickness。现在我需要在一个单独的应用程序中以Rect的形式绘制这些墙,首先从视图位置0,0开始,然后每个下一个应该将自己附加到前一个墙的末端并向右方向移动。当从上到下或从左到右绘制Rect(墙)时,我没有问题 enter image description here

我认为因为我设法得到那些Rects的正确起点。问题是如何绘制需要从下到上或从右到左绘制的Rects,如下所示: enter image description here

这是使用Rect提供画布的方法 请记住,第一个startX和Y是0,0,并且方法收到的宽度和高度是正确的顺序,例如,如果我们有4个墙,它将从1到4。

public void drawWall (int width,int height,int wallListSize){

    wall = new Rect();
    wall.set(startX,startY,width+startX,height+startY);

    wallList.add(wall);

    Log.i(TAG, "drawWall: Rect bottom:"+wall.bottom+" right:"+wall.right+" top:"+wall.top+" left:"+wall.left);

    //setting up starting position for next wall
    if (chooseDirection(width)){
        //vertical
        if (wall.bottom>startY){
            // up to down stroke
            Log.i(TAG, "drawWall: up to down stroke");
            startY = wall.bottom;
            startX = wall.left;

        }else {
            //down to up stroke
            Log.i(TAG, "drawWall: down to up stroke");
            startY = wall.top;
            startX = wall.right;

           // startX = wall
        }
    }else {
        //horizontal
        if (wall.right>startX){
            //left to right stroke
            Log.i(TAG, "drawWall: left to right stroke");
            startX = wall.right;
            startY = wall.bottom;
        }else {
            //right to left stroke
            Log.i(TAG, "drawWall: right to left stroke");
            startX = wall.left;
            startY = wall.top;

        }
    }
    //Checking if there is more walls or not
    Log.i(TAG, "drawWall: ---STARTING POSITION--- startX "+startX+" startY "+startY+" ");
    if (wallIndex < wallListSize){
        Log.i(TAG, "drawWall: incrementing index");
        wallIndex++;

    }else {
        Log.i(TAG, "drawWall: end of drawing, matching layout to wrap content");
        setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
    }
}

private boolean chooseDirection(int width){
    if (width == wallThickness){
        return true;
    }else {
        return false;
    }
}

1 个答案:

答案 0 :(得分:-1)

找到答案,在决定是什么样的中风时必须采取不同的方法。如果我从服务器发送起点和终点,就能得到它。

 public void drawWall (int width,int height,int wallListSize,int x1,int x2,int y1,int y2){

    wall = new Rect();
    //setting up starting position for next wall
    if (chooseDirection(width)){
        //vertical
        if (y1<y2){
            // up to down stroke

            wall.set(startX,startY,wallThickness+startX,height+startY);
            startY = wall.height();
           // startX=startX+wallThickness;
            Log.i(TAG, "drawWall: up to down stroke");


        }else  {
            //down to up stroke

            wall.set(startX,height-startY,wallThickness+startX,startY);
            startY = startY - wall.height();
           // startX = startX + wallThickness;
            Log.i(TAG, "drawWall: down to up stroke");


           // startX = wall
        }
    }else {
        //horizontal
        if (x1<x2){
            //left to right stroke
            Log.i(TAG, "drawWall: left to right stroke");
            wall.set(startX,startY,width+startX,wallThickness+startY);
            startX = wall.width();
           // startY = startY+wallThickness;
        }else {
            //right to left stroke
            wall.set(width-startX,startY,startX,wallThickness+startY);
            Log.i(TAG, "drawWall: right to left stroke");
            startX = startX-wall.width();
           // startY = startY+wallThickness;
        }
    }
    //adding wall to the pool
    wallList.add(wall);

    //Checking if there is more walls or not
    Log.i(TAG, "drawWall: Rect bottom:"+wall.bottom+" right:"+wall.right+" top:"+wall.top+" left:"+wall.left);
    Log.i(TAG, "drawWall: ---STARTING POSITION FOR NEXT WALL--- startX "+startX+" startY "+startY+" ");
    if (wallIndex < wallListSize){
        wallIndex++;

    }else {
        Log.i(TAG, "drawWall: end of drawing, matching layout to wrap content");
        startDraw = true;
        invalidate();
    }
}