2013-08-16 1 views
0

내 문제는 Inflater에 의해 호출되는 Layout 클래스가 있고이 클래스의 메서드를 실행하여 몇 초마다 그림을 업데이트하려고한다는 것입니다.레이아웃 클래스 업데이트

핸들러와 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); 
} 
} 

편집 :

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); 
} 
} 
+0

postDelayed (this, 0)는 의문의 여지가 있습니다. 왜 그냥 handler.post (this)가 아닌가? –

+0

모르겠다. 다른 클래스에서 복사했다. 시간을 늘리거나 줄이면 작동한다. 하지만 정말로 중요하지 않다고 생각하니? – UndeadLeech

+0

또는 더 나은 아직 0 이외의 지연 기간을 사용하십시오. 당신은 중장비 처리 중입니다. –

답변

0

을 대신 당신이 invalidate()를 호출해야 Runnable을 게시의 : 당신이 그것을보고 싶어하기 때문에

다음

Sprite 클래스 레이아웃을 다시 그릴 필요가있을 때. 또한 Sprite의 상태를 onDraw 방법으로 업데이트해야합니다.

+0

답변 주셔서 감사합니다. 내 게임 클래스의 Runnable에서 invalidate()를 사용하고 onDraw()에 업데이트 메서드를 추가 했으므로 이제는 작동하는 것 같습니다! 도움을 주셔서 감사합니다. 존재하는 아이디어가없는 경우 사용해야하는 방법에 대한 정보를 얻는 것은 꽤 어렵습니다. – UndeadLeech

+0

@ ChrisDürr 일반적으로 최소한 클래스에 대한 문서를 읽어야합니다. 그래, ** 정말 ** 큰데, 나는 그것을 읽는 것이 좋습니다. 궁극적으로 안드로이드에서 어떻게 작동 하는지를 아는 것이 얼마나 도움이되고 흔히 저지르는 실수를 피하게 할 지 놀라게 될 것입니다. –