Android动态壁纸中的动画?

时间:2011-02-06 14:06:10

标签: android animation canvas live-wallpaper

我是一个相当新的开发者,并试图制作一个动态壁纸应用程序。在许多动画中,我的第一个目标是显示一个实际上是黑洞的旋转位图。

    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;
    }
  }
}

所以我试图旋转位图并再次显示它,所以它看起来像它的吸吮星星和所有。 此外,我已经删除了基本onPause onResume函数,以便您可以轻松地理解代码。我知道我遗失了一些基本的东西,但是什么?

1 个答案:

答案 0 :(得分:0)

嗯。我现在需要花费更多的时间来解决这个问题,但我确实有一个建议:使用更简单的方法对壁纸进行编码。废弃线程并在Cube示例的行中做更多的事情,也就是说使用postDelayed制作一个runnable调度自己的调用。希望这可以帮助。乔治。