2010-11-29 245 views
2

我想從動態壁紙教程中找到以下內容,我找到了here動態壁紙教程

/** 
* Do the actual drawing stuff 
*/ 
private void doDraw(Canvas canvas) { 
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), IMAGES[current]); 
    canvas.drawColor(Color.BLACK); 
    canvas.drawBitmap(b, 0, 0, null); 
    Log.d(TAG, "Drawing finished."); 
} 

/** 
* Update the animation, sprites or whatever. 
* If there is nothing to animate set the wait 
* attribute of the thread to true 
*/ 
private void updatePhysics() { 
    // if nothing was updated : 
    // this.wait = true; 
    if(previousTime - System.currentTimeMillis() >= 41) { //24 FPS 
     current = current < IMAGES.length ? current++ : 0; 
    } 
    Log.d(TAG, "Updated physics."); 
} 

但它似乎沒有工作。我究竟做錯了什麼。 「繪製完成」。和「更新物理學」。消息正在打印。但我只看到第一張圖片。我正在模擬器上測試它。

任何幫助,將不勝感激。謝謝

+2

嗯,這是什麼`previousTime`包含哪些內容?我想你預計這個變量會被更新。調試器如果在「if(previousTime ...)」上放置斷點並檢查表達式,它會說什麼? – mreichelt 2010-11-29 10:40:19

回答

10

我已經制定了一個簡單的樣本動態壁紙,其中的顏色隨着時間的推移。也許你可以使用這個爲出發點:

package com.cmwmobile.android.samples; 

import android.graphics.Canvas; 

import android.os.Handler; 

import android.service.wallpaper.WallpaperService; 

import android.view.SurfaceHolder; 

/** 
* The SampleLiveWallpaperService class is responsible for showing the 
* animation and is an interface to android. 
* @author Casper Wakkers - www.cmwmobile.com 
*/ 
public class SampleLiveWallpaperService extends WallpaperService { 
    private Handler handler = null; 

    /** 
    * Inner class representing the actual implementation of the 
    * Live Wallpaper {@link Engine}. 
    */ 
    private class SampleLiveWallpaperEngine extends Engine { 
     private boolean visible = false; 

     private int[] colors = {0, 0, 0} ; 

     /** 
     * Runnable implementation for the actual work. 
     */ 
     private final Runnable runnableSomething = new Runnable() { 
      /** 
      * {@inheritDoc} 
      */ 
      public void run() { 
       drawSomething(); 
      } 
     }; 
     /** 
     * The drawSomething method is responsible for drawing the animation. 
     */ 
     private void drawSomething() { 
      final SurfaceHolder holder = getSurfaceHolder(); 

      Canvas canvas = null; 

      try { 
       canvas = holder.lockCanvas(); 

       if (canvas != null) { 
        canvas.drawARGB(200, colors[0], colors[1], colors[2]); 
       } 

       updateColors(colors); 
      } 
      finally { 
       if (canvas != null) { 
        holder.unlockCanvasAndPost(canvas); 
       } 
      } 

      // Reschedule the next redraw. 
      handler.removeCallbacks(runnableSomething); 

      if (visible) { 
       // Play around with the delay for an optimal result. 
       handler.postDelayed(runnableSomething, 25); 
      } 
     } 
     /** 
     * Method updateColors updates the colors by increasing the value 
     * per RGB. The values are reset to zero if the maximum value is 
     * reached. 
     * @param colors to be updated. 
     */ 
     private void updateColors(int[] colors) { 
      if (colors[0] < 255) { 
       colors[0]++; 
      } 
      else { 
       if (colors[1] < 255) { 
        colors[1]++; 
       } 
       else { 
        if (colors[2] < 255) { 
         colors[2]++; 
        } 
        else { 
         colors[0] = 0; 
         colors[1] = 0; 
         colors[2] = 0; 
        } 
       } 
      } 
     } 
     /** 
     * {@inheritDoc} 
     */ 
     public void onDestroy() { 
      super.onDestroy(); 

      handler.removeCallbacks(runnableSomething); 
     } 
     /** 
     * {@inheritDoc} 
     */ 
     public void onVisibilityChanged(boolean visible) { 
      super.onVisibilityChanged(visible); 

      this.visible = visible; 

      if (visible) { 
       drawSomething(); 
      } 
      else { 
       handler.removeCallbacks(runnableSomething); 
      } 
     } 
    } 

    /** 
    * Constructor. Creates the {@link Handler}. 
    */ 
    public SampleLiveWallpaperService() { 
     handler = new Handler(); 
    } 
    /** 
    * {@inheritDoc} 
    */ 
    public Engine onCreateEngine() { 
     return new SampleLiveWallpaperEngine(); 
    } 
}