0

I am new to android programming and have run into a small problem. If you could help, that would be greatly appreciated.

I am making rectangles for a live wallpaper I am making using:

void drawFrame() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                Paint paint = new Paint();
                paint.setColor(Color.CYAN);
                Paint ypaint = new Paint();
                ypaint.setColor(Color.RED);
                Paint ppaint = new Paint();
                ppaint.setColor(Color.GREEN);

                drawVerStripes(c, ppaint, 0,20);
                drawVerStripes(c, paint, 50,20);
                drawVerStripes(c, ypaint,0,10);
            }

        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }

The function:

Params:
width: width of the rectangle
space: the difference between last rect and this rect

    int y=0;
    int oldY=0;
    private void drawVerStripes(Canvas c, Paint paint, int space, int width) {


        y=oldY+space;

        c.drawRect(y, 0, y+width,c.getHeight(), paint);

        oldY=y;
    }

The result is the rectangles moving to one side of screen really fast. I want them to stay on screen and not move.
In other words, is there a way that drawVerStripes is executed only once and not every frame.

Gero K
  • 1
  • 2

2 Answers2

0

Your drawFrame method is called with each frame. If your devices refresh rate is 70, then your method is called 70 times a second. If you increase x or y coordinate by 1, then you move 70 pixels in either x or y coordinates. You should do refresh rate independent movement.

1) Calculate delta time which is the time that passed since the last screen update. 2) Determine how many pixels you want your rectangle to move every second.

Let's say you want you rectangle to move 50 pixels in x.

Then do the following. x += deltaTime * 50.

long startTime = System.nanoTime(); // Execute before the first time your drawFrame executes
float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f; // In drawFrame method calculate
long startTime = System.nanoTime(); // Execute after calculating delta time. 
neo
  • 1,952
  • 2
  • 19
  • 39
  • Then just have a boolean variable called ranOnce. Then set it to false. And wrap your calls to drawVerStripes in if(!ranOnce). Also after calls to drawVerStripes set ranOnce = true – neo Jun 11 '13 at 15:35
  • dont know why, but after doing that, the rects dont show up... `boolean ranOnce=false; if(!ranOnce){ drawVerStripes(c, ppaint, 0,20); drawVerStripes(c, paint, 50,20); drawVerStripes(c, ypaint,0,10); ranOnce = true; }` – Gero K Jun 11 '13 at 15:51
  • Because the screen being refreshed. You have to draw them with each refresh. Otherwise they are not drawn. – neo Jun 11 '13 at 15:52
  • Do the following pass a new parameter of type boolean to your drawVerStripes method called updateRequired. Wrap your y=oldY+space; in if(updateRequired) but leave drawRect as it is, because rectangle should be drawn each time with same coordinates. – neo Jun 11 '13 at 15:56
  • Why don't you just pass the y value instead of space? That way you don't need to hold global count of what y is. Currently it should be drawing 3 rectangles, but they must be overlapping. That's why you see only one. – neo Jun 12 '13 at 13:57
  • What you try to draw is the following right? One Rectangle on the left side of the screen that has width 20 and height the full screen and another starting from top of the screen width 20 going from x (50 to 70) and another going from x (50 to 60). The second rectangle you draw contains the third rectangle inside of it. Know that the coordinate system starts from the top left of the screen. Y coordinate goes downward and increasing. – neo Jun 12 '13 at 14:25
  • actually, I wasnt able to test it because it was moving, but in theory I think this is right:`y = oldY + space+width ; c.drawRect(y-width, 0, y, c.getHeight(), paint);` – Gero K Jun 12 '13 at 14:27
  • Because this kinda wasnt workin I tried to make a pref screen so the user would enter the space and width and I will add up and make the y cord that is needed but that became a problem in itself, see this: http://stackoverflow.com/questions/17066566/add-to-prefscreen-android – Gero K Jun 12 '13 at 14:34
  • Anyway you don't seem to know what you need. That's all help I can give. – neo Jun 12 '13 at 14:48
0

I believe that you need to set y and oldY to 0 again at the beginning of drawFrame.

Mauro
  • 381
  • 1
  • 6