i recently tried using a SurfaceView to draw a large collection of
circles to a Canvas arranged in a ring shape. just to prototype the
idea i originally wrote some code that effectively went like this:

                        Paint paint = new Paint();
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(1);
                        paint.setAntiAlias(true);
                        paint.setStrokeCap(Cap.ROUND);
                        paint.setColor(0xffffffff);

                        int circleSize = 20;
                        int circleCount = 50;
                        double theta = 0;
                        double thetaDelta = 2*Math.PI/circleCount;
                        int ringRadius = 100;
                        for(int idx = 0; idx < circleCount; idx++){
                                float x = (float)(ringRadius * Math.cos(theta) 
+ (surfaceWidth -
circleSize)/2);
                                float y = (float)(ringRadius * Math.sin(theta) 
+ (surfaceHeight -
circleSize)/2);
                                surfaceCanvas.drawCircle(x + circleSize/2, y + 
circleSize/2,
circleSize/2 - 2, paint);
                                theta += thetaDelta;
                        }

[this code sits in a "draw" method being called by a non-ui thread]

for the number of iterations above it works great. but as
"circleCount" gets higher, the performance penalty of
canvas.drawCircle() because noticeable.

so, i decided to try using a sprite instead. i changed the code to
draw a single circle to a bitmap first, then draw that bitmap to the
canvas for each iteration instead. here's the new code:

                        Paint paint = new Paint();
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(1);
                        paint.setAntiAlias(true);
                        paint.setStrokeCap(Cap.ROUND);
                        paint.setColor(0xffffffff);

                        int circleSize = 20;
                        int circleCount = 50;
                        Bitmap sprite = Bitmap.createBitmap(circleSize, 
circleSize,
Config.ARGB_8888);
                        Canvas spriteCanvas = new Canvas(sprite);
                        spriteCanvas.drawCircle(circleSize/2, circleSize/2, 
circleSize/2 -
2, paint);

                        double theta = 0;
                        double thetaDelta = 2*Math.PI/circleCount;
                        int ringRadius = 100;
                        for(int idx = 0; idx < circleCount; idx++){
                                float x = (float)(ringRadius * Math.cos(theta) 
+ (surfaceWidth -
circleSize)/2);
                                float y = (float)(ringRadius * Math.sin(theta) 
+ (surfaceHeight -
circleSize)/2);
                                surfaceCanvas.drawBitmap(sprite, x, y, null);
                                theta += thetaDelta;
                        }
                        sprite.recycle();
                        sprite = null;

this code performed significantly better ... but then i noticed that
there was something off about the visual result. it's a little hard to
explain, but it basically looked like there was a rounding error
happening when drawing the sprite to surfaceCanvas: the rendered
circles did not appear to be spaced evenly as they did in the initial
code.

here's a picture of what i get using the first code:
http://www.1908media.com/wp-content/shaperendered.jpg

here's a picture of what i get using the sprite code:
http://www.1908media.com/wp-content/spriterendered.jpg

i tried several things to get my sprite code to come out right ... the
only thing that eventually worked for me was to position *as well as
rotate* the sprite and paint it to the canvas using a separate Paint
object with anti-aliasing and filterBitmap enabled. i implemented this
using canvas.drawBitmap( bmp, matrix, paint ). but this incurred an
even greater performance penalty than canvas.drawCircle()!

i'm only barely knowledgeable of openGL, so hoping someone can
enlighten me as to what's not right with the second code block
above ... thoughts anyone?

fwiw, i saw the same results on my n1 running 2.2 as i did on an
emulator running 1.6

tia//


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to