0

I am currently having trouble with a live wallpaper that does some very intensive drawing. However , upon testing my code I find the wallpaper is laggy and is destroyed when the screen gets rotated.

Within my draw method I have this :

mRectArray = new Rect[] {
                new Rect(0, 0, c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 2),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 2,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 3),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 3,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 4),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 4,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 5),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 5,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 6),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 6,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 7),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 7,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 8),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 8,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 9),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 9,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 10),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 10,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 11),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 11,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 12),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 12,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 13),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 13,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 14),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 14,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 15),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 15,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 16),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 16,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 17),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 17,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 18),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 18,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 19),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 19,
                        c.getWidth(),
                        (c.getHeight() + statusBarHeight) / 20 * 20),

                new Rect(0, 0, right1,
                        (c.getHeight() + statusBarHeight) / 20),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20, right2,
                        (c.getHeight() + statusBarHeight) / 20 * 2),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 2,
                        right3, (c.getHeight() + statusBarHeight) / 20 * 3),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 3,
                        right4, (c.getHeight() + statusBarHeight) / 20 * 4),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 4,
                        right5, (c.getHeight() + statusBarHeight) / 20 * 5),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 5,
                        right6, (c.getHeight() + statusBarHeight) / 20 * 6),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 6,
                        right7, (c.getHeight() + statusBarHeight) / 20 * 7),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 7,
                        right8, (c.getHeight() + statusBarHeight) / 20 * 8),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 8,
                        right9, (c.getHeight() + statusBarHeight) / 20 * 9),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 9,
                        right10,
                        (c.getHeight() + statusBarHeight) / 20 * 10),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 10,
                        right11,
                        (c.getHeight() + statusBarHeight) / 20 * 11),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 11,
                        right12,
                        (c.getHeight() + statusBarHeight) / 20 * 12),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 12,
                        right13,
                        (c.getHeight() + statusBarHeight) / 20 * 13),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 13,
                        right14,
                        (c.getHeight() + statusBarHeight) / 20 * 14),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 14,
                        right15,
                        (c.getHeight() + statusBarHeight) / 20 * 15),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 15,
                        right16,
                        (c.getHeight() + statusBarHeight) / 20 * 16),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 16,
                        right17,
                        (c.getHeight() + statusBarHeight) / 20 * 17),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 17,
                        right18,
                        (c.getHeight() + statusBarHeight) / 20 * 18),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 18,
                        right19,
                        (c.getHeight() + statusBarHeight) / 20 * 19),
                new Rect(0, (c.getHeight() + statusBarHeight) / 20 * 19,
                        right20,
                        (c.getHeight() + statusBarHeight) / 20 * 20),

                new Rect(c.getWidth() / 20, c.getHeight() / 15,
                        c.getWidth() / 20 * 8, c.getHeight() / 15 * 4),

                new Rect(c.getWidth() / 20, c.getHeight() / 15,
                        c.getWidth() / 20 * 8, c.getHeight() / 15 * 8), };

That's 40 rectangle constantly being initialized onDraw. Now, the problem is these rectangles must be constantly initialized as i'm animating the right side of 20 of the rectangles...

I have another 40 or so if statements for animating the rectangles..

How can I condense my code so that performance isn't sacrificed??

Denizen
  • 335
  • 5
  • 17
  • try to use the profiler to find the bottleneck. also, can't you create the Rect once and then change its properties? creating an object is relatively expensive. also think about opengl, renderscript, etc. – Ran Jun 15 '12 at 16:21
  • the last 20 rectangle have variables for their right side and im animating each variable, but when I try drawing each rectangle after initializing them once in a method, i get a nullpointerexception and a force close. – Denizen Jun 16 '12 at 06:27

1 Answers1

0

initialize your array in onSurfaceChange() by using height and width of onSurfaceChange() function.

Ravi Bhojani
  • 1,032
  • 1
  • 13
  • 24