/as_03/src/jp/ac/oit/is/android_shooting_03/GameView.java
https://bitbucket.org/kmiyawaki/android_shooting · Java · 126 lines · 94 code · 15 blank · 17 comment · 0 complexity · 1572a940e2b32fdb11a392cecfbbf8c2 MD5 · raw file
- /**
- *
- */
- package jp.ac.oit.is.android_shooting_03;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Calendar;
- import java.util.Timer;
- import java.util.TimerTask;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.Paint.FontMetrics;
- import android.os.Handler;
- import android.view.View;
-
- /**
- * @author User
- *
- */
- public class GameView extends View {
- protected long _tm;
- protected long _delayMillis = (int) (1000 / 30);
- protected Handler _handler = null;
-
- // http://case-mobile-design.com/devices/resolution-list/
- public static final int DEFAULT_SCREEN_WIDTH = 720;
- public static final int DEFAULT_FONT_SIZE = 64;
- protected float _screenScale = 1.0f;
- protected Paint _paint = new Paint();
- protected int _bgColor = Color.rgb(239, 228, 176);
-
- private static final String CHARACTER_IMG = "k6_p1b.png";
- protected Bitmap _charaImage = null;
- protected int _charaX = 0;
- protected int _charaY = 0;
-
- public GameView(Context context) {
- super(context);
- // TODO ???????????????????
-
- // http://ichitcltk.hustle.ne.jp/gudon/modules/pico_rd/index.php?content_id=76
- // http://wiki.livedoor.jp/moonlight_aska/d/%A5%BF%A5%B9%A5%AF%A4%F2%C4%EA%B4%FC%C5%AA%A4%CB%BC%C2%B9%D4%A4%B9%A4%EB
- _handler = new Handler();
- Timer timer = new Timer(true);// ???????
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- _handler.post(new Runnable() {
- public void run() {
- // ???????
- invalidate();
- }
- });
- }
- };
- // ?????????
- timer.schedule(task, 0, _delayMillis);
- }
-
- protected void drawCharacter(Canvas c) {
- c.drawBitmap(_charaImage, _charaX, _charaY, _paint);
- }
-
- @Override
- public void onWindowFocusChanged(boolean hasWindowFocus) {
- int sw = getWidth();
- int sh = getHeight();
- _screenScale = sw / (float) DEFAULT_SCREEN_WIDTH;
- setDefaultFont();
-
- // ????????
- Bitmap src = loadImage(CHARACTER_IMG);
- Matrix matrix = new Matrix();
- matrix.postScale(_screenScale, _screenScale);
- int w = src.getWidth();
- int h = src.getHeight();
- _charaImage = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);
- // ??????
- _charaX = (sw - _charaImage.getWidth()) / 2;
- _charaY = sh - _charaImage.getHeight();
-
- // TODO ???????????????
- super.onWindowFocusChanged(hasWindowFocus);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawColor(_bgColor);
- FontMetrics fm = _paint.getFontMetrics();
- canvas.drawText(getTimeStr(), 0, 0 - fm.top, _paint);
- drawCharacter(canvas);
- }
-
- protected Bitmap loadImage(String file) {
- InputStream is = null;
- try {
- is = getResources().getAssets().open(file);
- } catch (IOException e) {
- // TODO ??????? catch ????
- e.printStackTrace();
- }
- return BitmapFactory.decodeStream(is);
- }
-
- protected void setDefaultFont() {
- int fontSize = (int) (DEFAULT_FONT_SIZE * _screenScale);
- _paint.setAntiAlias(false);
- _paint.setColor(Color.BLACK);
- _paint.setTextSize(fontSize);// ??????????
- }
-
- private String getTimeStr() {
- Calendar cal = Calendar.getInstance();
- String str = String.format("%02d:%02d:%02d.%03d",
- cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
- cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND));
- return str;
- }
- }