如何以编程方式连续绘制几个矩形?

时间:2013-12-16 12:45:17

标签: java android bitmap android-canvas

我想把几个矩形连成一排。但是因为我是Android新手,特别是Bitmap, Canvas等等,我需要一些帮助。

它应该看起来像这样,只有矩形:

enter image description here

我使用以下代码创建了一个矩形:

        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#CD5C5C"));
        Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bg); 
        canvas.drawRect(50, 80, 200, 200, paint); 
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);

        ImageView iV = new ImageView(this);
        iV.setImageBitmap(bg);

        ll.addView(iV);

但现在我不知道如何在一行中创建更多不同颜色的矩形。 我真的很新,对不起这个可能很愚蠢的问题,但我需要帮助。

有人能指导我如何以最好的方式做到这一点吗?

3 个答案:

答案 0 :(得分:1)

这里的关键是:

paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawRect(50, 80, 200, 200, paint);

他们设置颜色并绘制一个矩形。您现在可以重复这些行以获得2个矩形:

paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawRect(50, 80, 200, 200, paint);
paint.setColor(Color.parseColor("#DDDDDD"));
canvas.drawRect(210, 80, 360, 200, paint);

请注意,我已经改变颜色并稍微协调一下。您可以继续这样做几次以绘制所有矩形。

最好还是使用变量作为x和y坐标,并使用循环:

int left = 50; // initial start position of rectangles (50 pixels from left)
int top = 50; // 50 pixels from the top
int width = 150;
int height = 150;
for (int row = 0; row < 2; row++) { // draw 2 rows
    for(int col = 0; col < 4; col++) { // draw 4 columns
        paint.setColor(Color.parseColor("#CD5C5C"));
        canvas.drawRect(left, top, left+width, top+height, paint);
        left = (left + width + 10); // set new left co-ordinate + 10 pixel gap
        // Do other things here
        // i.e. change colour
    }
    top = top + height + 10; // move to new row by changing the top co-ordinate
}

希望有所帮助。

答案 1 :(得分:1)

这应该这样做。我试图尽可能地自我记录我的代码。这是非常动态的,即您可以调整高度,宽度,xPad,yPad等,窗口将进行补偿。

enter image description here

import java.awt.*;
import java.util.Random;
import javax.swing.*;

public class RectanglesPanel extends JPanel {    
    public static final int[] COLORS = new int[] {
        0xFFFFFF, 0xF67457, 0xFFC238, 0xEFEF38,
        0xBCCACA, 0x75D1E0, 0x84E0C2, 0xC2E749
    };

    private static Random rand = new Random();
    private int width = 80;
    private int height = 50;
    private int rows = 2;
    private int cols = 4;
    private int xPad = 20;
    private int yPad = 30;
    private float strokeWidth = 2.0f;

    int windowWidth = calculateOffset(width, cols, xPad);
    int windowHeight = calculateOffset(height, rows, yPad);

    public RectanglesPanel() {
        setPreferredSize(new Dimension(windowWidth, windowHeight));
    }

    private int calculateOffset(int whole, int partitions, int padding) {
        return (whole * partitions) + (padding * (partitions + 1));
    }

    @Override
    public void paintComponent(Graphics g) {
        Stroke stroke = new BasicStroke(strokeWidth,
                BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
        ((Graphics2D)g).setStroke(stroke);

        // Fill in background.
        g.setColor(new Color(0xF6F6F6));
        g.fillRect(0, 0, windowWidth, windowHeight);

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                int x = calculateOffset(width, col, xPad);
                int y = calculateOffset(height, row, yPad);
                int color = (row * cols + col) % COLORS.length;

                // Fill in rectangle.
                g.setColor(new Color(COLORS[color]));
                g.fillRect(x, y, width, height); 

                // Stroke the border of the rectangle.
                g.setColor(new Color(0xE7E7E7));
                g.drawRect(x, y, width, height);
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new RectanglesPanel();

        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

答案 2 :(得分:0)

用于循环保持y坐标不变

for(i=0;i<=200;i=i+40)
{
 canvas.drawRect(i,0,i+30,100);
}

下一行按需要的数量增加y坐标并重复相同或使用嵌套for循环

您可以通过

设置颜色
myPaint.setColor(color.black);
myPaint.setStyle(Style.FILL);
canvas.drawRect(0,0,100,100, myPaint);