Here's a small way that I was able to improve my drawing in a app I
have where I do lots of canvas calls. I also use an ArrayList. This is
the code in my onDraw():


temparray = trackEvents.toArray();

                int tracksize = trackEvents.size();

                if ((trackEvents != null) && (tracksize > 0))
                {


                        for (int counter = 0; counter < tracksize; counter++)
                        {



First I convert the arraylist to an array, then I precache the size,
then I loop thru it and treat it like a normal array using []
indexers.

-niko

On Oct 14, 3:00 am, Andy <[email protected]> wrote:
> HI Joel,
>
> drawing in a Canvas can be difficult from a performance approach.
>
> You can speed things up, here are only a few sidenotes you can check:
>
> * Do you preload Images
> * Which format are you using (ARGB 8888, ARGB 4444 or RGB565) : need
> Alpha ?
> * Are you allocating memory in you loop
> * Do you use any Kind of Collection ? = bad
>
> ....could be sum up more, but have to go to a meeting =)
>
> On 14 Okt., 03:21, Leigh McRae <[email protected]> wrote:
>
>
>
> >   I can't help but smile at this one since there was a thread not that
> > long ago talking about how people should just use OpenGL.  This yet
> > again shows how useful it would be if the native bitmap canvas calls
> > could be accelerated.
>
> > Anyway here are some ideas:
>
> > - Do some kind of screen space culling.
> > - Unroll the loop.  Not sure if the compiler does it.  If you know there
> > are 100 then maybe make a loop that does 5-10 per loop.  Even if you
> > only know it's always even then do two per loop.
> > - If the images don't always move/update then you could render to a
> > bitmap and use it as a cache.  If it's something like a tiled game then
> > layers could be cached and marked dirty.  I do this.
> > - use OpenGL :(
>
> > On 10/13/2010 7:46 PM, JoelDuggan wrote:
>
> > > Hey all,
>
> > > I'm looking for some help optimizing some bitmap drawing code.
>
> > > Here's my pseudo-code:
>
> > > draw() {
>
> > > lockCanvas()
>
> > > drawbackgroundBitmap()
>
> > > for (100times) {
> > >   drawbitmap(x,y);
> > > }
>
> > > unlockCanvas()
>
> > > }
>
> > > That inner loop is my bottleneck.  It is taking 160ms to run the whole
> > > function.  80% of that is spent making the drawBitmap calls in the
> > > loop.  Is there a faster method to draw the bitmaps.  The bitmaps are
> > > basically a fifo (actually an Arraylist) where new ones are added and
> > > old ones are removed.  Would openGL help?
>
> > > Thanks in advance.
>
> > --
> > Leigh McRaewww.lonedwarfgames.com

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

Reply via email to