0

私の問題は、Layoutによって呼び出されるクラスがInflaterあり、このクラスからメソッドを実行して、数秒ごとに画像を更新したいということです。

ハンドラーとメソッドでこれを行いたかったのですrun()が、私の問題は、画面を操作している (唯一のボタンをクリックしている) ときにのみ画像が更新されることです。

私が何を間違えたか知っていますか?

これが私のGameLayoutクラスです:

package de.undeadleech.frogjump;

public class GameLayout extends View implements Runnable
{
private Sprite sprite;
private Bitmap bmp;
private Handler handler = new Handler();

public GameLayout(Context context) 
{
    super(context);
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.froschanimation);
    sprite = new Sprite(bmp, 0, 0, 400, 100, 5, 4);
    handler.postDelayed(this, 0);
}

@Override
protected void onDraw(Canvas canvas)
{
    sprite.draw(canvas);
}

public void update(long currentTimeMillis) 
{
    sprite.update(currentTimeMillis);
}

@Override
public void run()
{
    sprite.update(System.currentTimeMillis());
    handler.postDelayed(this,  0);
}
}

編集:

Spriteあなたがそれを見たかったので、ここに私のクラスがあります:

package de.undeadleech.frogjump;



public class Sprite 
{   
//private static final String TAG = Sprite.class.getSimpleName();

private Bitmap bitmap;      // the animation sequence
private Rect sourceRect;    // the rectangle to be drawn from the animation bitmap
private int frameNr;        // number of frames in animation
private int currentFrame;   // the current frame
private long frameTicker;   // the time of the last frame update
private int framePeriod;    // milliseconds between each frame (1000/fps)

private int spriteWidth;    // the width of the sprite to calculate the cut out rectangle
private int spriteHeight;   // the height of the sprite

private int x;              // the X coordinate of the object (top left of the image)
private int y;              // the Y coordinate of the object (top left of the image)

public Sprite(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) 
{
    this.bitmap = bitmap;
    this.x = x;
    this.y = y;
    currentFrame = 0;
    frameNr = frameCount;
    spriteWidth = bitmap.getWidth() / frameCount;
    spriteHeight = bitmap.getHeight();
    sourceRect = new Rect( 0, 0, spriteWidth, spriteHeight);
    framePeriod = 1000 / fps;
    frameTicker = 0l;
}

public void update(long gameTime) 
{
    if (gameTime > frameTicker + framePeriod) 
    {
        frameTicker = gameTime;
        // increment the frame
        currentFrame++;
        if (currentFrame >= frameNr) 
        {
            currentFrame = 0;
        }
    }
    // define the rectangle to cut out sprite
    this.sourceRect.left = currentFrame * spriteWidth;
    this.sourceRect.right = this.sourceRect.left + spriteWidth;
}

public void draw(Canvas canvas) 
{
    // where to draw the sprite
    Rect destRect = new Rect( x, y, x + spriteWidth, y + spriteHeight);
    canvas.drawBitmap(bitmap, sourceRect, destRect, null);
}
}
4

1 に答える 1

0

レイアウトを再描画する必要がある場合は、投稿する代わりにRunnable呼び出す必要があります。また、 inメソッドinvalidate()の状態も更新する必要があります。SpriteonDraw

于 2013-08-16T21:45:32.163 に答える