0

when i try to change the screen color from black, i use the command drawARGB once, and i intend it to stay that color i asked, and other drawings will occur on top of it, but it only shows once, for a split second, and turns black again...

here is the code, if u wonder it all works fine, other than the drawARGB...

this is part of a live wallpaper.

public void drawFrame() {
        // TODO Auto-generated method stub
       c = null;
       try {
           c = holder.lockCanvas();
           if (c != null) {
               // draw something
            height = c.getHeight();
            width = c.getWidth();
            drawScreenColor();
            frame++;
           }
       } finally {
           if (c != null) holder.unlockCanvasAndPost(c);
       }
         mHandler.removeCallbacks(mDrawStrip);
            if (clearToRun) {
                mHandler.postDelayed(mDrawStrip, 1000 / 60);
            }
    }

    void drawScreenColor() {

        if (isFirstFrame) {
            c.drawARGB(255,255,0,0);
            isFirstFrame = false;
        }

    }
Daniel Mendel
  • 506
  • 5
  • 17

1 Answers1

0

This is happening due to your "first frame" code.

if (isFirstFrame) {
    c.drawARGB(255,255,0,0);
    isFirstFrame = false;
}

So, this will execute once, set isFirstFrame to false, and never execute again. The way a Canvas works is that all elements must be redrawn every frame (elements from the previous frame do not stick around). So, remove the isFirstFrame check, and make this the first call in your drawFrame method; everything following it will be drawn on top.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • I know that, but u don't understand, i want it to be drawn once, why than, when i draw random bitmaps or lines (drawLine), every frame that is everytime it calls, and in random positions, they do stay on the canvas, unless drawn on them? – Daniel Mendel Jul 11 '12 at 16:54
  • You'd have to show that part of the code. There's no reason they'd stick around, unless you didn't call `invalidate()`. – Cat Jul 11 '12 at 17:28
  • my other part is just another function that just has drawLine(x,y,endx,endy,paint) (the x and y are changing every time), that's all... and it does stick to the canvas...this is a service (live wallpaper) after all, i am not sure if i can call invalidate. – Daniel Mendel Jul 11 '12 at 17:40
  • Thing is, the `Canvas` should be cleared on the beginning of every frame. (Even in wallpapers; you can see that demoed [here](http://www.vogella.com/articles/AndroidLiveWallpaper/article.html), where he redraws every circle every frame.) The idea is that all the items you want on the `Canvas` would be redrawn each frame. – Cat Jul 11 '12 at 18:07
  • but it's a bit stupid, for instance what if i have a large back ground image right? that stays the same with out change the entire life cycle, and i only add small bitmap sprites around it, that only they need to be updated, it would just take a bunch of memory only to draw that background image every time... is there a smart way to do that? – Daniel Mendel Jul 11 '12 at 18:20
  • Well let's see... the internet is actually providing mixed solutions. Most say to retain the bitmap and redraw it (in the event that it's a background), but some posts say that [things drawn should be automatically retained](http://stackoverflow.com/questions/5110913/setting-background-bitmap-in-live-wallpaper). – Cat Jul 11 '12 at 18:37