2016-09-05 10 views
0

あなたは長方形のリストを描く方法をアドバイスできますか?複数の矩形を描く

私はRectangleクラスがあります。

public final class Rectangle extends View { 

    private Rect rectangle; 
    private Paint paint; 

    public Rectangle(Context context) { 
     super(context); 
     int x = 50; 
     int y = 50; 
     int sideLength = 200; 

     // create a rectangle that we'll draw later 
     rectangle = new Rect(x, y, sideLength, sideLength); 

     // create the Paint and set its color 
     paint = new Paint(); 
     paint.setColor(Color.GRAY); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     //canvas.drawColor(Color.BLUE); 
     canvas.drawRect(rectangle, paint); 
    } 

} 

と活動上の四角形のインスタンスを

ここ
setContentView(new Rectangle(this)); 

答えて

0

あなたが持っている側のいくつかの例3の長方形側

public class Rectangles extends View { 

    private Paint paintBlack; 

    public Rectangles(Context context) { 
     super(context); 
     init(); 
    } 

    public Rectangles(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public Rectangles(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private void init() { 
     paintBlack = new Paint(); 
     paintBlack.setColor(Color.BLACK); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawRect(0, 0, 100, 100, paintBlack); 
     canvas.drawRect(105, 0, 205, 100, paintBlack); 
     canvas.drawRect(210, 0, 310, 100, paintBlack); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(500, 500); 
    } 
}