1

so I'm just starting to learn how to create live wallpapers in eclipse and I'm having trouble getting a simple line to move randomly across the screen after a random amount of time, sort of like a shooting star. I think my stop and start is wrong also... I was trying to set a length limit for the line... I'm using the CubeLiveWallpaper as a template

    /*
     * Draw a line
     */
    void drawCube(Canvas c) {
        c.save();
        c.drawColor(0xff000000);
        drawLine(c);
        c.restore();
    }

    /*
     * Line path
     */
    void drawLine(Canvas c) {

        // Move line across screen randomly


        // 
        float startX = 0;
        float startY = 0;
        float stopX =  100;
        float stopY =  100;

        c.drawLine(startX, startY, stopX, stopY, mPaint);
    }
Amanda S
  • 3,266
  • 4
  • 33
  • 45
MJ93
  • 4,966
  • 8
  • 32
  • 50
  • i can get the line to appear on the screen but i want it to randomly shoot across the screen – MJ93 Jun 22 '11 at 23:20

1 Answers1

1

This is a pretty open-ended question. I'll try to give you some pointers. :-)

First of all, with all due respect to our good buddies at Google, the Cube example does not always present "best practice." Most notably, you should "never" use hard-coded constants in your wallpaper...always use a proportion of your screen size. In most cases, it's "good enough" to save the width and height variables from onSurfaceChanged() into class variables. My point is, instead of "100," you should be using things like "mScreenWidth / 4" to indicate one quarter of the width of your device (be it teeny tiny phone or ginormous tablet).

To get random numbers, you can use http://developer.android.com/reference/java/util/Random.html

As for the animation itself, well, you can randomize the rate by randomizing the delay you use to reschedule your runnable in postDelayed().

By now, you're probably wondering about the "tricky" part...drawing the line itself. :-) I suggest starting with something very simple, and adding complexity as you eyeball things. Let's say, fr'instance you generate random start and finish points, so that your final stroke will be

c.drawLine(startX, startY, stopX, stopY, mPaint);

Presumably, you will want to draw a straight line, which means maintaining a constant slope. You could set up a floating point "percentage" variable, initialized to zero, and each time through the runnable, increment it by a random amount, so that at each pass it indicates the "percentage" of the line you wish to draw. So each call in your runnable would look like

c.drawLine(startX, startY, startX + percentage * deltaX, startY + percentage * deltaX * slope, mPaint);

(where deltaX = stopX - startX)

Obviously, you want to stop when you hit 100 percent.

This is really just a start. You can get as heavy-duty with your animation as you wish (easing, etc.), for instance using a library like this one: http://code.google.com/p/java-universal-tween-engine/

Another option, depending on the effect you're trying to achieve, would be to work with a game engine, like AndEngine. Again, pretty heavy duty. :-) http://code.google.com/p/andenginelivewallpaperextensionexample/source/browse/

Good luck!

George Freeman
  • 2,260
  • 1
  • 15
  • 22