2011-02-06 183 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