본문 바로가기

Android/Sprite Animation 02

Android Sprite Animation example

안드로이드 스프라이트 애니메이션 예, Part 02

앞서 작성된 파일은 여기를 참조하세요                                                                                               

Part 03 은 여기를 참조하세요

사용된 스프라이트 시트  

spritesheet.zip

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onBtnStart" android:layout_weight="1" android:text="Start Animation" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onBtnStop" android:layout_weight="1" android:text="Stop Animation" />

</LinearLayout> <com.example.androidapp.SpriteView android:id="@+id/spriteView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>


MainActivity.java

package com.example.androidapp; import android.app.*; import android.content.*; import android.graphics.*; import android.os.*; import android.util.*; import android.view.*; import android.view.*; import android.view.View.*; import android.widget.*; public class MainActivity extends Activity { boolean go; SpriteView spriteView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spriteView = (SpriteView) findViewById(R.id.spriteView); } public void onBtnStart(View v) { spriteView.startAnimation(); } public void onBtnStop(View v){ spriteView.stopAnimation(); } }


SpriteView.java

package com.example.androidapp; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; /* spritesheet.png (1440x1480), 5행6열 */ public class SpriteView extends View {

int frameWidth; int frameHeight; Bitmap spriteSheet = BitmapFactory.decodeResource(getResources(), R.drawable.spritesheet); Rect src = new Rect(); Rect dst = new Rect(); int x, y; boolean go; SpriteThread spriteThread; public SpriteView(Context context) { super(context); init(); } public SpriteView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init(){ frameWidth = spriteSheet.getWidth()/6; frameHeight = spriteSheet.getHeight()/5; dst.left = dst.top = 0; dst.right = frameWidth; dst.bottom = frameHeight; } public void startAnimation() { go = true; spriteThread = new SpriteThread(); spriteThread.start(); } public void stopAnimation() { go = false; try { spriteThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(spriteSheet,src, dst, null); } /* Sprite Sheet에서 읽어와야할 프레임 이미지의 좌표를 계산하는 쓰레드 */ public class SpriteThread extends Thread { int screenWidth, screenHeight; int speed = 5; @Override public void run() { screenWidth = getWidth(); screenHeight = getHeight(); while(go) { for(int i=0;i<5;i++) { for(int k=0;k<6;k++) { src.left = k*frameWidth; src.top = i*frameHeight; src.right = src.left+frameWidth; src.bottom = src.top+frameHeight; dst.left = x; dst.top = 0; dst.right = dst.left+frameWidth; dst.bottom = dst.top+frameHeight; postInvalidate(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } }// end of inner for() }// end of outer for() }// end of while() }// end of run() }// end of class SpriteThread }