Android实时壁纸中的动画?

Android实时壁纸中的动画?,android,animation,canvas,live-wallpaper,Android,Animation,Canvas,Live Wallpaper,我是一个相当新的开发人员,正在尝试制作一个实时壁纸应用程序。在许多动画中,我的第一个目标是显示一个旋转位图,它实际上是一个黑洞 public class Painting extends Thread { /** Reference to the View and the context */ private SurfaceHolder surfaceHolder; private Context context; /** State */ p

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

    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函数,这样你们就可以很容易地理解代码。我知道我遗漏了一些基本的东西,但是什么呢?

嗯。要想弄清楚这一点,我需要比现在更多的时间,但我有一个建议:用一种更简单的方法为你的壁纸编码。废弃线程,按照多维数据集示例的思路做更多的事情,也就是说,创建一个runnable,它使用postDelayed调度对自身的调用。希望这有帮助。乔治。

我之所以发布线程代码,只是因为这里似乎存在主要问题。谢谢乔治。我已经解决了这件事。我真的很感谢你抽出时间。