336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
출처 : http://mobile.dzone.com/articles/beginning-android-game?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Findic+(Indic+Zone)
자세한 알고리즘이나 기본 골격은 위 사이트 참고 하시면 됩니다.
역시 무슨 공부를 해도 있는 거 그대로 배끼면 절대 머리에 안들어옵니다.
위 예제에서 약간 변형을 해 보았습니다.
변경된 부분을 살짝쿵 예기 드리면
AnimatedSprite을 저장 할 수 있는 ArrayList를 두어서 onTouchEvent 이벤트가 발생 되는 지점에 폭팔 애니메이션을 그려주게끔 변경을 해보았네요.
일단 타이머를 두거나 폭팔 횟수에 따라서 지워줘야 하는데 이건 귀찮아서리...ㅋ
위 사이트 참고 하시면서 보시면 이해가 빠르실 겁니다.
AnimatedSprite class
package com.adroid.tutorial;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class AnimatedSprite {
private Bitmap animation;
private int xPos;
private int yPos;
private Rect sRectangle;
private int fps;
private int numFrames;
private int currentFrame;
private long frameTimer;
private int spriteHeight;
private int spriteWidth;
public AnimatedSprite() {
sRectangle = new Rect (0, 0, 0, 0);
frameTimer = 0;
currentFrame = 0;
xPos = 80;
yPos = 200;
}
public AnimatedSprite(int xPos, int yPos) {
sRectangle = new Rect (0, 0, 0, 0);
frameTimer = 0;
currentFrame = 0;
this.xPos = xPos;
this.yPos = yPos;
}
public void Initialize (Bitmap bitmap, int height, int width, int fps, int frameCount) {
this.animation = bitmap;
this.spriteHeight = height;
this.spriteWidth = width;
this.sRectangle.top = 0;
this.sRectangle.bottom = spriteHeight;
this.sRectangle.left = 0;
this.sRectangle.right = spriteWidth;
this.fps = 1000/fps;
this.numFrames = frameCount;
}
public int getXPos() {
return xPos;
}
public int getYPos() {
return yPos;
}
public void setXPos(int value) {
xPos = value;
}
public void setYPos(int value) {
yPos = value;
}
public void Update(long gameTime) {
if( gameTime > frameTimer + fps) {
frameTimer = gameTime;
currentFrame += 1;
if( currentFrame >= numFrames ) {
currentFrame = 0;
}
sRectangle.left = currentFrame * spriteWidth;
sRectangle.right = sRectangle.left + spriteWidth;
}
}
public void draw(Canvas canvas) {
Rect dest = new Rect(getXPos(), getYPos(), getXPos() + spriteWidth,
getYPos() + spriteHeight);
//canvas.drawColor(Color.BLACK);
canvas.drawBitmap(animation, sRectangle, dest, null);
}
}
AnimationThread class
package com.adroid.tutorial;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class AnimationThread extends Thread {
private SurfaceHolder surfaceHolder;
private ISurface panel;
private boolean run = false;
public AnimationThread(SurfaceHolder surfaceHolder, ISurface panel) {
this.surfaceHolder = surfaceHolder;
this.panel = panel;
panel.onInitalize();
}
public void setRunning(boolean value) {
run = value;
}
private long timer;
@Override
public void run() {
Canvas canvas;
while (run) {
canvas = null;
timer = System.currentTimeMillis();
panel.onUpdate(timer);
try {
canvas = surfaceHolder.lockCanvas();
if (null == canvas)
break;
synchronized (surfaceHolder) {
panel.onDraw(canvas);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
DrawablePanel class
package com.adroid.tutorial;
import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public abstract class DrawablePanel extends SurfaceView implements SurfaceHolder.Callback, ISurface{
private AnimationThread thread;
SurfaceHolder mHolder;
public DrawablePanel(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
this.thread = new AnimationThread(mHolder, this);
}
@Override
public void onDraw(Canvas canvas) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
ISurface interface
package com.adroid.tutorial;
import android.graphics.Canvas;
public interface ISurface {
void onInitalize();
void onDraw(Canvas canvas);
void onUpdate(long gameTime);
}
AndroidTutorial class
package com.adroid.tutorial;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
public class AndroidTutorial extends Activity {
ArrayList arSprite = new ArrayList();
final static int FRAME = 7;
final static int FPS = 14;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new AndroidTutorialPanel(this));
}
class AndroidTutorialPanel extends DrawablePanel {
public AndroidTutorialPanel(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int idx=0; idx < arSprite.size(); idx++) {
arSprite.get(idx).draw(canvas);
}
}
@Override
public void onInitalize() {
}
@Override
public void onUpdate(long gameTime) {
for (int idx=0; idx < arSprite.size(); idx++) {
arSprite.get(idx).Update(gameTime);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
AnimatedSprite aniSprite = new AnimatedSprite();
aniSprite.Initialize(
BitmapFactory.decodeResource(
getResources(),
R.drawable.explosion),
32, 32, FPS, FRAME);
aniSprite.setXPos((int)event.getX() - 16);
aniSprite.setYPos((int)event.getY() - 16);
arSprite.add(aniSprite);
return true;
}
return false;
}
}
}
'ⓐndroid > reference' 카테고리의 다른 글
| 안드로이드 효과음 출력 (Creating Sound Effects in Android) (0) | 2011.03.09 |
|---|---|
| 안드로이드 날씨 어플 만들기(1) - android simple weather application (0) | 2011.03.04 |
| Android TabHost Tutorial – Part 2 (0) | 2011.01.22 |
| Android TabHost Tutorial – Part 1 (0) | 2011.01.22 |
| Activity 호출 2 - 다른 프로그램의 Activity 호출하기 (0) | 2011.01.22 |