/as_03/src/jp/ac/oit/is/android_shooting_03/GameView.java
Java | 126 lines | 94 code | 15 blank | 17 comment | 0 complexity | 1572a940e2b32fdb11a392cecfbbf8c2 MD5 | raw file
1/**
2 *
3 */
4package jp.ac.oit.is.android_shooting_03;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.Calendar;
9import java.util.Timer;
10import java.util.TimerTask;
11
12import android.content.Context;
13import android.graphics.Bitmap;
14import android.graphics.BitmapFactory;
15import android.graphics.Canvas;
16import android.graphics.Color;
17import android.graphics.Matrix;
18import android.graphics.Paint;
19import android.graphics.Paint.FontMetrics;
20import android.os.Handler;
21import android.view.View;
22
23/**
24 * @author User
25 *
26 */
27public class GameView extends View {
28 protected long _tm;
29 protected long _delayMillis = (int) (1000 / 30);
30 protected Handler _handler = null;
31
32 // http://case-mobile-design.com/devices/resolution-list/
33 public static final int DEFAULT_SCREEN_WIDTH = 720;
34 public static final int DEFAULT_FONT_SIZE = 64;
35 protected float _screenScale = 1.0f;
36 protected Paint _paint = new Paint();
37 protected int _bgColor = Color.rgb(239, 228, 176);
38
39 private static final String CHARACTER_IMG = "k6_p1b.png";
40 protected Bitmap _charaImage = null;
41 protected int _charaX = 0;
42 protected int _charaY = 0;
43
44 public GameView(Context context) {
45 super(context);
46 // TODO ???????????????????
47
48 // http://ichitcltk.hustle.ne.jp/gudon/modules/pico_rd/index.php?content_id=76
49 // 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
50 _handler = new Handler();
51 Timer timer = new Timer(true);// ???????
52 TimerTask task = new TimerTask() {
53 @Override
54 public void run() {
55 _handler.post(new Runnable() {
56 public void run() {
57 // ???????
58 invalidate();
59 }
60 });
61 }
62 };
63 // ?????????
64 timer.schedule(task, 0, _delayMillis);
65 }
66
67 protected void drawCharacter(Canvas c) {
68 c.drawBitmap(_charaImage, _charaX, _charaY, _paint);
69 }
70
71 @Override
72 public void onWindowFocusChanged(boolean hasWindowFocus) {
73 int sw = getWidth();
74 int sh = getHeight();
75 _screenScale = sw / (float) DEFAULT_SCREEN_WIDTH;
76 setDefaultFont();
77
78 // ????????
79 Bitmap src = loadImage(CHARACTER_IMG);
80 Matrix matrix = new Matrix();
81 matrix.postScale(_screenScale, _screenScale);
82 int w = src.getWidth();
83 int h = src.getHeight();
84 _charaImage = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);
85 // ??????
86 _charaX = (sw - _charaImage.getWidth()) / 2;
87 _charaY = sh - _charaImage.getHeight();
88
89 // TODO ???????????????
90 super.onWindowFocusChanged(hasWindowFocus);
91 }
92
93 @Override
94 protected void onDraw(Canvas canvas) {
95 canvas.drawColor(_bgColor);
96 FontMetrics fm = _paint.getFontMetrics();
97 canvas.drawText(getTimeStr(), 0, 0 - fm.top, _paint);
98 drawCharacter(canvas);
99 }
100
101 protected Bitmap loadImage(String file) {
102 InputStream is = null;
103 try {
104 is = getResources().getAssets().open(file);
105 } catch (IOException e) {
106 // TODO ??????? catch ????
107 e.printStackTrace();
108 }
109 return BitmapFactory.decodeStream(is);
110 }
111
112 protected void setDefaultFont() {
113 int fontSize = (int) (DEFAULT_FONT_SIZE * _screenScale);
114 _paint.setAntiAlias(false);
115 _paint.setColor(Color.BLACK);
116 _paint.setTextSize(fontSize);// ??????????
117 }
118
119 private String getTimeStr() {
120 Calendar cal = Calendar.getInstance();
121 String str = String.format("%02d:%02d:%02d.%03d",
122 cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
123 cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND));
124 return str;
125 }
126}