androidでviewを利用して画像を表示
要はあれですゲーム系
デザインはゴミカスですけど、まあちょっとは作ってみたい欲望とかあるわけなのでいろいろと参考書を見て形だけとりかかってみました。
アクティビティを使わない
Viewのクラスを作成してそれを画面として利用するようです。
実装
Activity
package jp.mshige1979.app.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new GameView(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
GameView
package jp.mshige1979.app.myapplication; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * Created by matsumotoshigeharu on 15/01/22. */ public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable{ private SurfaceHolder holder; private Thread thread; private Bitmap bgBitMap; private Bitmap ballBitMap; private int posBallX = 0; private int posBallY = 0; private int posBallSX = 10; // 移動速度 private int posBallSY = 10; // 移動速度 public GameView(Context context){ super(context); // 背景画像 bgBitMap = readBitmap(context, "bg"); // ボール ballBitMap = readBitmap(context, "ball"); // サーフェスホルダーの処理 holder = getHolder(); holder.addCallback(this); holder.setFixedSize(480, 800); // ボールの初期位置 posBallX = 100; posBallY = 600; } // サーフェス開始時の処理 public void surfaceCreated(SurfaceHolder holder){ thread = new Thread(this); thread.start(); } // サーフェス変更時の処理 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){ } // サーフェス終了時の処理 public void surfaceDestroyed(SurfaceHolder holder){ thread = null; } // スレッドの処理 public void run(){ Canvas canvas; Paint paint = new Paint(); while(null != thread){ // lock? canvas = holder.lockCanvas(); // 背景を描画 canvas.drawBitmap(bgBitMap, 0, 0, null); // ボールの描画 canvas.drawBitmap(ballBitMap, posBallX - 14, posBallY - 14, null); // 文字列の描画 paint.setColor(Color.BLUE); paint.setTextSize(30); canvas.drawText("Test String", 10, 40, paint); // ボール移動量を変更 posBallX += posBallSX; posBallY += posBallSY; if(posBallX + 14 > 480 - 14){ posBallSX = posBallSX * -1; } if(posBallX < 14){ posBallSX = posBallSX * -1; } if(posBallY + 14 > 800 - 14){ posBallSY = posBallSY * -1; } if(posBallY < 14){ posBallSY = posBallSY * -1; } // unlock? holder.unlockCanvasAndPost(canvas); // 一時sleep try{ Thread.sleep(40); }catch(Exception e){ } } } private Bitmap readBitmap(Context context, String name){ int res_id = context.getResources().getIdentifier(name, "drawable", context.getPackageName()); return BitmapFactory.decodeResource(context.getResources(), res_id); } }
結果
※とりあえず、やっつけ
所感
画像の読み込み処理とかをうまくやって対応することで作成できるよう
最初はブロック崩しとかのサンプルをみてから作成してみるのがいいかも…
仕事で勉強していてもなんかうまく理解できないので家でゆっくりやるほうが落ち着く
参考
- 作者: 布留川英一
- 出版社/メーカー: ソシム
- 発売日: 2015/01/26
- メディア: 単行本
- この商品を含むブログ (1件) を見る