/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

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