我认为因为我设法得到那些Rects的正确起点。问题是如何绘制需要从下到上或从右到左绘制的Rects,如下所示:
这是使用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;
}
}
答案 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();
}
}