2011-02-06 2 views
0

Я довольно новый разработчик и пытаюсь создать приложение для живых обоев. Среди многих анимаций моя первая цель - показать вращающийся битмап, который на самом деле является черной дырой.Анимация в Live Wallpaper 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

ответ

0

Хммм. Это займет больше времени, чем сейчас, чтобы разобраться в этом, но у меня есть одно предложение: закодируйте обои, используя более простой подход. Отбросьте поток и сделайте что-нибудь еще по строкам примера Cube, то есть сделайте runnable, который планирует вызовы для себя, используя postDelayed. Надеюсь это поможет. Джордж.

+0

Спасибо, Джордж. Я решил это. Я очень ценю ваше время. – JehandadK

Смежные вопросы