2011-02-06 244 views
0

我是一個相當新的開發人員,並試圖做一個動態壁紙應用程序。在許多動畫中,我的第一個目標是顯示一個實際上是黑洞的旋轉位圖。動態壁紙Android?

public class Painting extends Thread { 

    /** Reference to the View and the context */ 
    private SurfaceHolder surfaceHolder; 
    private Context context; 

    /** State */ 
    private boolean wait; 
    private boolean run; 

    /** Dimensions */ 
    private int width; 
    private int height; 

    /** Time tracking */ 
    private long previousTime; 

    boolean first = true; 

    Bitmap hole; 

    int degree; 
    public Painting(Context con , SurfaceHolder surf) 
    { 
      context = con; 
      surfaceHolder = surf; 
      this.wait = true; 
      Log.i("Live Test","UnInitialized"); 
      Drawable d = (con.getResources().getDrawable(R.drawable.vlack)); 
      hole = ((BitmapDrawable)d).getBitmap(); 
      hole.prepareToDraw(); 
      if(hole != null) 
      Log.i("Live Test","Initialized"); 
      run = true;wait = false; 
      degree = 0; 

    } 

    @Override 
    public void run() 
    { 
      while (run) { 
        this.run = true; 
        Canvas c = null; 


        Log.i("Live Test","Draw Color"); 
        while (run) { 
          try { 
            c = this.surfaceHolder.lockCanvas(); 
            synchronized (this.surfaceHolder) { 
              doDraw(c); 
            } 
          } finally { 
            if (c != null) { 
              this.surfaceHolder.unlockCanvasAndPost(c); 
              Log.i("Live Test","Unlocked And Posted"); 
            } 
          } 
          // pause if no need to animate 
          synchronized (this) { 
            if (wait) { 
              try { 
                wait(); 
              } catch (Exception e) { 
                Log.i("Live Test","Error wait"); 
              } 
            } 
          } 
        } 
      } 

    } 

    public void setSurfaceSize(int width, int height) { 
      this.width = width; 
      this.height = height; 
      synchronized(this) { 
        this.notify(); 
      } 
    } 


    /** 
* Pauses the livewallpaper animation 
*/ 
public void pausePainting() { 
    this.wait = true; 
    synchronized(this) { 
     this.notify(); 
    } 
} 

/** 
* Resume the livewallpaper animation 
*/ 
public void resumePainting() { 
    this.wait = false; 
    synchronized(this) { 
     this.notify(); 
    } 
} 

/** 
* Stop the livewallpaper animation 
*/ 
public void stopPainting() { 
    this.run = false; 
    synchronized(this) { 
     this.notify(); 
    } 
} 


    private void doDraw(Canvas canvas) { 
      if(first) 
      { 
        canvas.save(); 
        canvas.drawColor(0x60444444); 
        canvas.drawBitmap(hole, 80,80,null); 
        canvas.restore(); 
        first = false; 
      } 
      else 
      { 
      long currentTime = System.currentTimeMillis(); 
      long elapsed = currentTime - previousTime; 
      if (elapsed > 20) { 

      canvas.save(); 
      degree+= 5; 
      if(degree>359)degree = degree -358; 
      canvas.rotate((float) degree); 
      canvas.restore(); 
      Log.i("Live Test","rotated"); 
      } 
      previousTime = currentTime; 
    } 
    } 
} 

所以我想將旋轉位圖,並再次顯示它,所以它看起來像它吸吮明星和所有。 另外我已經刪除了Basic onPause onResume Functions,以便你們可以很容易地理解代碼。我知道我缺少一些基本的東西,但是什麼?

+0

我只發佈了線程代碼,因爲它似乎是主要問題在這裏。 – JehandadK 2011-02-06 14:07:12

回答

0

嗯。我現在需要花費更多的時間來解決這個問題,但我確實有一個建議:使用更簡單的方法編寫壁紙。按照Cube示例的方式廢棄線程並執行更多操作,也就是說創建一個可使用postDelayed調度自己的runnable。希望這可以幫助。喬治。

+0

ThankYou George。我已經解決了這個問題。我非常感謝你的時間。 – JehandadK 2011-03-18 03:51:11