0

ライブ壁紙を作っています。また、スプライトのフレーム変更をマルチスレッド化しようとすると問題が発生します。更新メソッドと描画メソッドがあります

public void update() {

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        currentFrame = ++currentFrame % 2;

    }

    public void Draw(Canvas c, int sx, int sy){
        update();
        x = sx;
        y = sy;
        int srcX =  currentFrame * width;
        int srcY = 0;
        Rect src = new Rect(srcX,srcY,srcX+width,srcY+height);
        Rect dst = new Rect(x,y,x+width,y+height);
        c.drawBitmap(b, src, dst, null);

クラスを作ってみました

public class SecondThread extends Thread{
            public void run(){
                seaweed.update();

            }
            }

これにより、独自のスレッドで更新が実行されるため、さまざまな時間の長さでスリープできます。1つは50スリープし、もう1つは500スリープします。SecondThreadクラスをspirteクラス内に配置すると、最初のフレームに設定されます。壁紙エンジンクラスを挿入して開始しようとすると、1つのステップのように動作し、クラッシュします。私はここで何が間違っているのですか?

4

1 に答える 1

0

int を更新した後、ビューでpostInvalidate()を呼び出してみてください。何かのようなもの:

public void update() {

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    currentFrame = ++currentFrame % 2;
    YourViewClass.this.postInvalidate();
}

public void Draw(Canvas c, int sx, int sy){
    update();
    x = sx;
    y = sy;
    int srcX =  currentFrame * width;
    int srcY = 0;
    Rect src = new Rect(srcX,srcY,srcX+width,srcY+height);
    Rect dst = new Rect(x,y,x+width,y+height);
    c.drawBitmap(b, src, dst, null);
于 2013-03-09T21:38:05.810 に答える