0

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

    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,以便你们可以轻松理解代码。我知道我缺少一些基本的东西,但是什么?

4

1 回答 1

0

嗯。解决这个问题需要比我现在更多的时间,但我确实有一个建议:使用更简单的方法对壁纸进行编码。废弃线程并按照 Cube 示例的方式做更多的事情,也就是说,使用 postDelayed 来安排对自身的调用的可运行对象。希望这可以帮助。乔治。

于 2011-03-18T03:25:37.927 回答