2015-10-15 18 views
0

我是android編程的新手,嘗試在使用畫布drawRect方法的android上創建5個矩形(兩個在屏幕右側和三個在屏幕左側),但它只顯示左上角的矩形。我在這裏做錯了什麼?謝謝你的幫助。用android UI畫布創建五個矩形

這裏是我的Rectangle類:

public class RectAngle extends View { 
    public RectAngle(Context context) { 
     super(context); 
     setFocusable(true); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     float x = getWidth(); 
     float y = getHeight(); 

     Paint topLeftRect = new Paint(); 
     Paint bottomLeftRect = new Paint(); 
     Paint topRightRect = new Paint(); 
     Paint midRightRect = new Paint(); 
     Paint bottomRightRect = new Paint(); 

     // Draw the top left rectangle 
     topLeftRect.setStyle(Paint.Style.FILL); 
     //topLeftRect.setColor(Color.WHITE); 
     topLeftRect.setColor(Color.parseColor("#FFFF99")); 
     // canvas.drawPaint(topLeftRect); 
     canvas.drawRect(0, 0, x/2, y/2, topLeftRect); 

     //Draw the bottom left rectangle 

     bottomLeftRect.setStyle(Paint.Style.FILL); 
     //bottomLeftRect.setColor(Color.WHITE); 
     // canvas.drawPaint(bottomLeftRect); 
     bottomLeftRect.setColor(Color.parseColor("#FFFF99")); 
     canvas.drawRect(0, y/2, x/2, 0, bottomLeftRect); 

     //Draw the top tight rectangle 

     topRightRect.setStyle(Paint.Style.FILL); 
     //topRightRect.setColor(Color.WHITE); 
     topRightRect.setColor(Color.parseColor("#FF6600")); 
     //canvas.drawPaint(topRightRect); 
     canvas.drawRect(x/2, 0, 0, y/3, topRightRect); 

     // Draw the middle right rectangle 

     midRightRect.setStyle(Paint.Style.FILL); 
     //midRightRect.setColor(Color.WHITE); 
     midRightRect.setColor(Color.parseColor("#66FFFF")); 
     // canvas.drawPaint(midRightRect); 
     canvas.drawRect(x/2, 0, 0, y/3, midRightRect); 

     //Draw the bottom right rectangle 

     bottomRightRect.setStyle(Paint.Style.FILL); 
     //bottomRightRect.setColor(Color.WHITE); 
     bottomRightRect.setColor(Color.parseColor("#CCCC00")); 
     // canvas.drawPaint(bottomRightRect); 
     canvas.drawRect(x/2, y/3, 0, 0, bottomRightRect); 




    } 
} 

這是我的主要活動:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new RectAngle(this)); 
     //setContentView(R.layout.activity_main); 
    } 

這裏是我的仿真器的圖像。我只得到一個彩色的矩形,它應該在屏幕的左下角,右上角,中間右下角和右下角再增加4個矩形。

Emulator image

+0

你想要什麼畫在畫布上?用你的問題添加圖片 – USKMobility

+0

Thanks USK。我添加了我的模擬器的屏幕截圖。 – HassanB

回答

0
public class RectAngle extends View { 
    public RectAngle(Context context) { 
     super(context); 
     setFocusable(true); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     float x = getWidth(); 
     float y = getHeight(); 

     Paint topLeftRect = new Paint(); 
     Paint bottomLeftRect = new Paint(); 
     Paint topRightRect = new Paint(); 
     Paint midRightRect = new Paint(); 
     Paint bottomRightRect = new Paint(); 

     // Draw the top left rectangle 
     topLeftRect.setStyle(Paint.Style.FILL); 
     //topLeftRect.setColor(Color.WHITE); 
     topLeftRect.setColor(Color.parseColor("#FFFF99")); 
     // canvas.drawPaint(topLeftRect); 
     canvas.drawRect(0, 0, x/2, y/2, topLeftRect); 

     //Draw the bottom left rectangle 

     bottomLeftRect.setStyle(Paint.Style.FILL); 
     //bottomLeftRect.setColor(Color.WHITE); 
     // canvas.drawPaint(bottomLeftRect); 
     bottomLeftRect.setColor(Color.parseColor("#FF5599")); 
     canvas.drawRect(0, y/2, x/2, y, bottomLeftRect); 

     //Draw the top tight rectangle 

     topRightRect.setStyle(Paint.Style.FILL); 
     //topRightRect.setColor(Color.WHITE); 
     topRightRect.setColor(Color.parseColor("#FF6600")); 
     //canvas.drawPaint(topRightRect); 
     canvas.drawRect(x/2, 0, x, y/3, topRightRect); 

     // Draw the middle right rectangle 

     midRightRect.setStyle(Paint.Style.FILL); 
     //midRightRect.setColor(Color.WHITE); 
     midRightRect.setColor(Color.parseColor("#66FFFF")); 
     // canvas.drawPaint(midRightRect); 
     canvas.drawRect(x/2, y/3, x, 2*y/3, midRightRect); 

     //Draw the bottom right rectangle 

     bottomRightRect.setStyle(Paint.Style.FILL); 
     //bottomRightRect.setColor(Color.WHITE); 
     bottomRightRect.setColor(Color.parseColor("#CCCC00")); 
     // canvas.drawPaint(bottomRightRect); 
     canvas.drawRect(x/2, 2*y/3, x, y, bottomRightRect); 




    } 
} 
+0

謝謝USK。所以,我的座標是錯誤的。再次感謝。 – HassanB