Android 动态壁纸中的动画?

发布于 2024-10-16 15:42:04 字数 3997 浏览 2 评论 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;
    }
  }
}

因此,我尝试旋转位图并再次显示它,使其看起来像吸吮星星之类的。 另外,我还删除了基本的 onPause onResume 函数,以便你们可以轻松理解代码。我知道我缺少一些基本的东西,但是什么?

I am fairly new developer and trying to make a live wallpaper app. Among many animations, my first target is to show a rotating Bitmap which is actually a Black Hole.

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

So I am trying to rotate the bitmap and show it again so it looks like its sucking stars and all.
Also I have removed Basic onPause onResume Functions so that you guys can understand the code easily. I know there is something basic I am missing, but What?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-10-23 15:42:04

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

Hmmm. It would take more time than I have right now to puzzle this out, but I do have one suggestion: code your wallpaper using a simpler approach. Scrap the thread and do something more along the lines of the Cube example, that is to say make a runnable which schedules calls to itself using postDelayed. Hope this helps. George.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文