开发者

Draw multiple shapes using loop - Android Canvas

I am very new to android development and have been trying to draw a square comprised of multiple smaller rectangles of different colours... Like a Mosaic essentially.

Basically at the moment I am reading values from a file which assigns the colour to the smaller Rects. I am using a pair of nested for loops to try to draw the small Rects sequentially, line by line. However when the program finishes there is only one small Rect drawn which is the last one to be drawn and its colour corresponds to the first value read from the file.

Here is some of my code to show you what I mean:

public SnapshotDraw(Context context) {
    super(context);

    for(int a = 0; a < 63; a++){
        for(int b = 0; b < 63; b++){
            fileName = PREFIX + "2" + EXTENSION;

   开发者_如何学Go         try {
                bf = new BufferedReader(new FileReader(fileName));
                tokens = new StringTokenizer(bf.readLine(), " \n");
                weight = Byte.parseByte(tokens.nextToken());

                x_scalar = b*MAG;
                y_scalar = a*MAG;   

                mDrawable = new ShapeDrawable(new RectShape());
                mDrawable.getPaint().setColor(colour.getColour(weight));
                mDrawable.setBounds((X_OFFSET + x_scalar), (Y_OFFSET + y_scalar), ((MAG + X_OFFSET) + x_scalar), ((MAG + Y_OFFSET) + y_scalar));

            } catch (FileNotFoundException ex) {
                Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);
}

This except is from a class which extends View and is called inside an onCreate() method in an Activity.

I would appreciate any guidance in this and thanks in advance!!

Cheers.


You are constructing the BufferedReader inside the loops, so bf.readLine() will always return the same line. Try moving bf and tokens (be aware that the use of StringTokenizer is discouraged) out of the loops.


Ok I got it sorted! Here is what I did to solve it:

public SnapshotDraw(Context context) {
    super(context);
    setFocusable(true);

    mBitmap = Bitmap.createBitmap(475, 720, Bitmap.Config.ALPHA_8);
}

@Override 
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);

    Paint p = new Paint();
    float y = 10;

    try {
        fileName = PREFIX + "2" + EXTENSION;
        bf = new BufferedReader(new FileReader(fileName));

        for(int a = 0; a < 63; a++){
            tokens = bf.readLine().split(" \n");
            for(int b = 0; b < 63; b++){

                weight = Byte.parseByte(tokens[b]);

                x_scalar = b*MAG;
                y_scalar = a*MAG;   

                p.setColor(new Colour().getColour(weight));
                canvas.drawRect((X_OFFSET + x_scalar), (Y_OFFSET + y_scalar), ((MAG + X_OFFSET) + x_scalar), ((MAG + Y_OFFSET) + y_scalar), p);
            }
        } 
    } catch (FileNotFoundException ex) {
        Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
    }

    canvas.drawBitmap(mBitmap, 10, y, p);
}

Much the same as before but changed the way I draw to the Bitmap. It looks beautiful btw!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜