/** Do the actual drawing stuff */
 private void doDraw(Canvas canvas) {
   canvas.drawColor(Color.BLACK);
   bkg.draw(canvas);
   sprCoin.draw(canvas);
   pipePlant.draw(canvas);
   sprPipe.draw(canvas);
   sprBlock.draw(canvas);
   mario.draw(canvas);
 }
  public void onOffsetsChanged(
      float xOffset,
      float yOffset,
      float xOffsetStep,
      float yOffsetStep,
      int xPixelOffset,
      int yPixelOffset) {

    bkg.setBkgShift(xPixelOffset);
    mario.setxOffset(0 /*_xPixelOffset - xPixelOffset*/);
    sprCoin.setBkgShift(xPixelOffset);
    sprPipe.setBkgShift(xPixelOffset);
    sprBlock.setBkgShift(xPixelOffset);
    pipePlant.setBkgShift(xPixelOffset);

    _xPixelOffset = xPixelOffset;

    /*for(int i=0;i<numSprites;i++) {
    	spriteList[i].setFgShift(xPixelOffset);
    }*/
  }
 @Override
 public void run() {
   this.run = true;
   Canvas c = null;
   while (run) {
     try {
       c = this.surfaceHolder.lockCanvas(null);
       synchronized (this.surfaceHolder) {
         currentTime = System.currentTimeMillis();
         if (currentTime > previousTime + mFPS || true) {
           previousTime = currentTime;
           updatePhysics();
           mario.update(currentTime);
           sprCoin.Update(currentTime);
           sprBlock.Update(currentTime);
           pipePlant.Update(currentTime);
           if (pipePlant.getYPos() <= this.pipePlantInitY) {
             pipePlant.setYVel(1);
           }
           if (pipePlant.getYPos() >= this.pipePlantInitY + 70) {
             pipePlant.setYVel(-1);
           }
           doDraw(c);
         }
       }
     } finally {
       if (c != null) {
         this.surfaceHolder.unlockCanvasAndPost(c);
       }
     }
     // pause if no need to animate
     synchronized (this) {
       if (wait) {
         try {
           wait();
         } catch (Exception e) {
         }
       }
     }
   }
 }
  public LiveWallpaperPainting(SurfaceHolder surfaceHolder, Context context) {
    // keep a reference of the context and the surface
    // the context is needed if you want to inflate
    // some resources from your livewallpaper .apk
    this.surfaceHolder = surfaceHolder;
    this.context = context;
    // don't animate until surface is created and displayed
    this.wait = true;
    mario = new Mario(64, 578, context);
    // spriteList = new AnimatedSprite[16];
    // numSprites = 0;

    sprCoin = new AnimatedSprite();
    sprPipe = new AnimatedSprite();
    sprBlock = new AnimatedSprite();
    pipePlant = new AnimatedSprite();
    bkg = new AnimatedSprite();

    sprCoin.setID(1);
    sprBlock.setID(2);

    sprPipe.Initialize(
        BitmapFactory.decodeResource(context.getResources(), R.drawable.pipe),
        72,
        48,
        1,
        false,
        -1.0f,
        AnimatedSprite.objects.Pipe);
    sprCoin.Initialize(
        BitmapFactory.decodeResource(context.getResources(), R.drawable.coin),
        24,
        20,
        4,
        true,
        10.0f,
        AnimatedSprite.objects.Coin);
    sprBlock.Initialize(
        BitmapFactory.decodeResource(context.getResources(), R.drawable.animblock),
        24,
        24,
        4,
        true,
        10.0f,
        AnimatedSprite.objects.CoinBlock);
    pipePlant.Initialize(
        BitmapFactory.decodeResource(context.getResources(), R.drawable.pipeplant),
        48,
        40,
        4,
        true,
        10.0f,
        AnimatedSprite.objects.Plant);
    bkg.Initialize(
        BitmapFactory.decodeResource(context.getResources(), R.drawable.wallpaper_bkg),
        1515,
        642,
        1,
        false,
        -1.0f,
        AnimatedSprite.objects.Bkg);

    sprPipe.setSolid(true);
    sprBlock.setSolid(true);

    // mario.addCollisionObj(sprPipe);
    mario.addCollisionObj(sprCoin);
    mario.addCollisionObj(sprBlock);
    mario.addCollisionObj(sprPipe);

    mSensor = new SensorActivity(context);
  }
  /** Invoke when the surface dimension change */
  public void setSurfaceSize(int width, int height) {
    this.width = width;
    this.height = height;

    bkg.setXPos(0);
    bkg.setYPos(height - 642 - 154);
    sprCoin.setYPos(500);
    sprCoin.setXPos(100);
    sprBlock.setYPos(500);
    sprBlock.setXPos(600);
    sprPipe.setXPos(324);
    sprPipe.setYPos(548);
    pipePlant.setXPos(328);
    pipePlant.setYPos(504);
    pipePlantInitY = pipePlant.getYPos();

    synchronized (this) {
      this.notify();
    }
  }