Reputation: 129
I'm currently working on making an Android live wallpaper. In this particular wallpaper, I have an array of circles generated from where the use touches. Among the settings of the app, I will allow them to change the resolution of said functions, as that pertains to the point of this. Therefore, I have written my own antialiased circle function.
Rendering is done through an effect similar to particles - I calculate the color of the rectangles I pass over with the circle, and render them accordingly. These rectangles store values for me, so I need to render each individually. I have done everything I can to speed this up - precalculate the Paint objects for the various colors (few enough that this is effective and still efficient), precalculated the rectangle' coordinates, etc. However, I'm still losing in speed at the render loop - a simple loop that iterates through any rectangle that is colored and applies it to the screen.
I do no operations but compare if the rectangle is colored, and the value of the rectangle directly pulls out which Paint object to use from an array. How can I gain speed here? I'm unable to increase the resolution beyond 16-pixel squares filling the screen without it becoming unbearably slow.
Here's the code fragment that is the bottleneck:
c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
for(int x = 0; x < sizex; x++)
for(int y = 0; y < sizey; y++){
if(tiles[x][y] != 0){
if(tiles[x][y] > 100)
tiles[x][y] = 100;
c.drawRect(rects[x][y], ps[tiles[x][y]]);//This seems to be the bottleneck
}
}
So, in essence, is there an alternate method to drawRect()
that would be faster?
Upvotes: 1
Views: 641
Reputation: 10057
Three things to consider:
Multi-dimensional array access is a little slower than single dimensional. But I think that's negligible (Java: Multi-dimensional array vs. One-dimensional)
Instead of drawRect
you might consider using drawLines
to draw all lines of all rects at once. This is faster than multiple calls to drawLine
. That's why I assume it's also faster than multiple drawRect
s. But I don't have any proof.
Drawing on canvas is slow. If performance matters you should use surface view (although I don't know if that works for LiveWallpapers). If performance matters or for particles and these kind of advanced graphics stuff, I strongly suggest using OpenGL, or some library like libGDX.
Upvotes: 1