Androidのゲームは、微信機大戦をまねる.


夏休みの実习の一つのプログラムは、javaとandroidの开発に初めて触れました.微信の飞行机大戦をまねて、効果図は次の通りです.
         
     
一:素材整理
素材はネットワークから来ました.ダウンロード先はここです.http://download.csdn.net/detail/bifei8718/7665447
二:出会った問題
Android Suifaceviewを使って、フラッシュスクリーン、ホームを押してから暗い画面に入るなど、開発時に多くの問題がありました.解決方法は他の転載記事の中にあります.
SurfaceView画面のフラッシュとダブルバッファ
surfaceviewダブルバッファ
【Androidゲーム開発19】(必見編)SurfaceView運営メカニズム詳細解―BackとHomeボタンを解析し、バックグラウンドに切り込むなどの異常処理!
三:java継承、派生、多形の使用
多くの敵機があるので、一つの基質を抽象化できます.Thingsの基質を定義しました.
(1)Thingsの基質
package com.swust.gamedeom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;

public abstract class Things {
	
	public float x ;
	public float y ;
	public float r; //   right
	public float b;//   buttom
	
	public float speed ;
	public float width;
	public float hight;
	
	public Bitmap  Image;
	public Resources res;
	public int  lifeValue;
	private int Sorces;
	
	public int getLifeValue() {
		return lifeValue;
	}

	public void setLifeValue(int lifeValue) {
		this.lifeValue = lifeValue;
	}

	public void lifeReduce(){
		lifeValue--;
	}
	public float getR() {
		return r;
	}
	public void setR(float r) {
		this.r = r;
	}
	public float getB() {
		return b;
	}
	public void setB(float b) {
		this.b = b;
	}
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	public float getWidth() {
		return width;
	}
	public void setWidth(float width) {
		this.width = width;
	}
	public float getHight() {
		return hight;
	}
	public void setHight(float hight) {
		this.hight = hight;
	}
	public void SetSpeed(float speed){
		this.speed = speed;
	}
	public RectF getrectf(){
		return new RectF(x, y, x+width, y+hight);
	}
	public int getSorces() {
		return Sorces;
	}
	public void setSorces(int sorces) {
		Sorces = sorces;
	}	

	public abstract void Draw(Canvas canvas);//  
	public abstract void Move();//    
	public abstract int check();//      
	public abstract void playsound(MainActivity act);//           

}
そして
それぞれ派生する
自分の飛行機
package com.swust.gamedeom;
import java.util.ArrayList;

import com.swust.gamedeom.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;


public class Plane extends Things{

	private Bitmap Image1;
	private Bitmap Image2;
	private Bitmap Image3;
	private Bitmap Image4;
	private int  num;
	public Plane(Resources r,float Swidth,float Sheight){
		res = r;
		Image = BitmapFactory.decodeResource(res, R.drawable.hero1);
		width = Image.getWidth();
		hight = Image.getHeight();
		setX((Swidth-width)/2);
		setY(Sheight-hight);
		setR(x+width);
		setB(y+hight);
		SetSpeed(10);
		setLifeValue(1);
		Init();
	}
	/**
	 *       
	 */
	public void Init(){
		Image1 = BitmapFactory.decodeResource(res, R.drawable.hero_blowup_n1);
		Image2 = BitmapFactory.decodeResource(res, R.drawable.hero_blowup_n2);
		Image3 = BitmapFactory.decodeResource(res, R.drawable.hero_blowup_n3);
		Image4 = BitmapFactory.decodeResource(res, R.drawable.hero_blowup_n4);
		num=0;
	}
	/**
	 *     
	 */
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(getLifeValue()==1){
			canvas.drawBitmap(Image, x, y,null);
		}
		else {
			num++;
			if(num==1){
				canvas.drawBitmap(Image1, x, y,null);
			}
			else if(num==2){
				canvas.drawBitmap(Image2, x, y,null);
			}
			else if(num==3){
				canvas.drawBitmap(Image3, x, y,null);
			}
			else if(num==4){
				canvas.drawBitmap(Image4, x, y,null);
			}
		}
		//Move();
	}
	@Override
	public void Move() {
		// TODO Auto-generated method stub
	}
	/**
	 *       
	 */
	public int check(){
		if(num>=4){
			return 1;
		}
		else {
			return 0;
		}
	}
	/**
	 *     
	 * @param EmPlanelist        
	 * @param act	    
	 */
	public void hitenemy( ArrayList EmPlanelist,MainActivity act){
		RectF rect1 = new RectF(x+15,y+15,x+width-15,y+hight-15);
		RectF rect2;
		for (int i = 0; i < EmPlanelist.size(); i++) {
			 Things emPlane = EmPlanelist.get(i);
			 rect2 = emPlane.getrectf();
			if(rect1.intersect(rect2)&& emPlane.getLifeValue()!=0&&this.getLifeValue()!=0){
				this.lifeReduce();
				emPlane.lifeReduce();
				act.stopSound();
				act.playSound(7, 0);
			}
		}
	}
	/**
	 * 
	 * @param ufo   
	 * @param act   
	 * @return        ,  true,  false
	 */
	public boolean GetUfo(Ufo ufo,MainActivity act){
		RectF rect1 = this.getrectf();
		RectF rect2 = ufo.getrectf();
		if(rect1.intersect(rect2)){
			ufo.lifeReduce();
			act.playSound(8, 0);
			return true;
		}
		return false;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
		act.playSound(7, 0);
	}
}
背景描画---BackGround
package com.swust.gamedeom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;

public  class BackGround extends Things{

	private Bitmap ImageStart;//      
	private Bitmap bitmap1;//       
	private Bitmap bitmap2;
	private Bitmap ImageOver;//      
	private Bitmap ImageAgain;//       
	private Bitmap ImageContinue;//     
	private Bitmap ImagePassue;//     
	private Bitmap bitmapover;//       
	private Bitmap LoadImage1;//     
	private Bitmap LoadImage2;
	private Bitmap LoadImage3;
	private Bitmap LoadImage4;
	private Bitmap titleImage;
	private Bitmap ImageMusicClose;//     
	private Bitmap ImageMusicOpen;
	private float top = 0;
	private int Statue;//    
	private int Score;//    
	private Paint paint;
	private int num = 0;
	private MainActivity act;
	private boolean flag;
	private int highScore;//   
	private boolean isplay;
	private Paint sorcePaint;
	
	public void setAct(MainActivity act) {
		this.act = act;
	}
	public void setSources(int Score) {
		this.Score = Score;
	}
	public int getStatue() {
		return Statue;
	}
	public void setStatue(int statue,boolean isplay) {
		Statue = statue;
		this.isplay = isplay;
	}
	public BackGround(Resources r,float SWidth,float SHeight,Paint paint){
		res =r;
		ImageAgain = BitmapFactory.decodeResource(res,R.drawable.game_again);
		ImageStart = BitmapFactory.decodeResource(res,R.drawable.game_start);
		ImageContinue = BitmapFactory.decodeResource(res,R.drawable.game_continue);
		ImageOver = BitmapFactory.decodeResource(res,R.drawable.game_over);
		ImagePassue = BitmapFactory.decodeResource(res,R.drawable.game_pause_nor);
		
		bitmap1 = CreatBitmap(SWidth, SHeight,R.drawable.background);
		bitmap2 = CreatBitmap(SWidth, SHeight,R.drawable.background);
		bitmapover = CreatBitmap(SWidth, SHeight,R.drawable.gameover);
		
		LoadImage1 = BitmapFactory.decodeResource(res,R.drawable.game_loading1);
		LoadImage2 = BitmapFactory.decodeResource(res,R.drawable.game_loading2);
		LoadImage3 = BitmapFactory.decodeResource(res,R.drawable.game_loading3);
		LoadImage4 = BitmapFactory.decodeResource(res,R.drawable.game_loading4);
		titleImage = BitmapFactory.decodeResource(res,R.drawable.shoot_copyright);
		
		ImageMusicClose = BitmapFactory.decodeResource(res,R.drawable.bkmusic_close);
		ImageMusicOpen = BitmapFactory.decodeResource(res,R.drawable.bkmusic_play);
		
		this.hight = SHeight;
		this.width = SWidth;
		this.paint = paint;
		
		flag = true;
		SetSpeed(3);
		isplay = true;
		
		sorcePaint = new Paint();
		Typeface font = Typeface.create(Typeface.SANS_SERIF,Typeface.BOLD);
		sorcePaint.setTypeface(font);
		sorcePaint.setColor(0xff949694);
		sorcePaint.setTextSize(75);	
	}
	/**
	 *                 
	 * @param SWidth      
	 * @param SHeight     
	 * @param ID    ID
	 * @return        
	 */
	
	public Bitmap CreatBitmap(float SWidth,float SHeight,int ID){
		Bitmap bitMap = BitmapFactory.decodeResource(res,ID);
		int widtht = bitMap.getWidth();
		int height = bitMap.getHeight();
		
		int newWidth = (int)SWidth;
		int newHeight =(int)SHeight;
		
		float scaleWidth = ((float) newWidth) / widtht;
		float scaleHeight = ((float) newHeight) / height;
		
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		
		bitMap = Bitmap.createBitmap(bitMap, 0, 0, widtht, height, matrix, true);
		return bitMap;
	}
	/**
	 *             
	 */
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(Statue == 1){
			canvas.drawBitmap(bitmap1, 0, top - hight, null);
			canvas.drawBitmap(bitmap2, 0, top, null);
			canvas.drawBitmap(ImagePassue, 0, 0, null);
			canvas.drawText(""+Score,ImagePassue.getWidth()+10, 45, paint);
			Move();
			flag = true;
		}
		else if(Statue == 2){
			 canvas.drawBitmap(bitmap1, 0, 0, null);
			 if(isplay){
				 canvas.drawBitmap(ImageMusicOpen, width-ImageMusicOpen.getWidth(), 0, null);
			 }
			 else{
				 canvas.drawBitmap(ImageMusicClose, width-ImageMusicClose.getWidth(), 0, null);
			 }
			 canvas.drawBitmap(ImageContinue, (width-ImageContinue.getWidth())/2,hight/2-150, null);
			 canvas.drawBitmap(ImageAgain, (width-ImageAgain.getWidth())/2,hight/2, null);
			 canvas.drawBitmap(ImageOver, (width-ImageOver.getWidth())/2,hight/2+150, null);
		}
		else if(Statue == 3){
			if(flag){	
				flag = false;	
				String string = SDcardData.ReadDate();
				highScore = Integer.valueOf(string).intValue();
				if(highScore=10 && num<20){
				 canvas.drawBitmap(LoadImage2, (width-LoadImage2.getWidth())/2, hight/2+50, null);
			 }
			 else if(num>=20&& num<30){
				 canvas.drawBitmap(LoadImage3, (width-LoadImage3.getWidth())/2, hight/2+50, null);
			 }
			 else if(num==30){
				 canvas.drawBitmap(LoadImage4, (width-LoadImage4.getWidth())/2, hight/2+50, null);
				 num = 0;
			 }
		}
	}
	/**
	 *       
	 */
	@Override
	public void Move() {
		// TODO Auto-generated method stub
		top += speed;
		if (top >= (hight + speed))
			top = 0;
	}
	@Override
	public int check() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
	}
}
敵機---EGmptyPlane
package com.swust.gamedeom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class EmptyPlane extends Things{

	private float Height;
	private int num;
	private Bitmap Image1;
	private Bitmap Image2;
	private Bitmap Image3;
	private Bitmap Image4;
	
	public EmptyPlane(Resources r,Float SWidth,float SHeight){
		this.res = r;
		Image = BitmapFactory.decodeResource(r, R.drawable.enemy1);
		width = Image.getWidth();
		hight = Image.getHeight();
		setX((float)(Math.random()*(SWidth-width)));
		setY(0);
		SetSpeed(7);//15
		this.Height = SHeight;
		setLifeValue(1);
		Init();
		setSorces(10);
	}
	public void Init(){
		Image1 = BitmapFactory.decodeResource(res, R.drawable.enemy1_down1);
		Image2 = BitmapFactory.decodeResource(res, R.drawable.enemy1_down2);
		Image3 = BitmapFactory.decodeResource(res, R.drawable.enemy1_down3);
		Image4 = BitmapFactory.decodeResource(res, R.drawable.enemy1_down4);
		num=0;
	}
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(getLifeValue()==1){
			canvas.drawBitmap(Image, x, y,null);
			Move();
		}
		else {
			switch (num/3) {
			case 0:
				canvas.drawBitmap(Image1, x, y,null);
				break;
			case 1:
				canvas.drawBitmap(Image2, x, y,null);
				break;
			case 2:
				canvas.drawBitmap(Image3, x, y,null);
				break;
			case 3:
				canvas.drawBitmap(Image4, x, y,null);
				break;
			default:
				break;
			}
			num++;
		}
	}
	@Override
	public void Move() {
		// TODO Auto-generated method stub
		y += speed;
	}
	@Override
	public int check(){
		if(num>=12){
			return 1;
		}
		if(y>Height){
			return 2;
		}
		return 0;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
		act.playSound(3, 0);
	}
}
敵機の大きな飛行機-- Em Big Plane
package com.swust.gamedeom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class EmBigPlane extends Things{
	
	private float Height;
	private int num;
	private Bitmap Image1;
	private Bitmap Image2;
	private Bitmap Image3;
	private Bitmap Image4;
	private Bitmap ImageHit;
	public EmBigPlane(Resources r,Float SWidth,float SHeight){
		this.res = r;
		Image = BitmapFactory.decodeResource(r, R.drawable.enemy2);
		ImageHit = BitmapFactory.decodeResource(r, R.drawable.enemy2_hit);
		width = Image.getWidth();
		hight = Image.getHeight();
		setX((float)(Math.random()*(SWidth-width)));
		setY(0);
		SetSpeed(5);//13
		this.Height = SHeight;
		setLifeValue(10);
		Init();
		setSorces(50);
	}
	public void Init(){
		Image1 = BitmapFactory.decodeResource(res, R.drawable.enemy2_down1);
		Image2 = BitmapFactory.decodeResource(res, R.drawable.enemy2_down2);
		Image3 = BitmapFactory.decodeResource(res, R.drawable.enemy2_down3);
		Image4 = BitmapFactory.decodeResource(res, R.drawable.enemy2_down4);
		num=0;
	}
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(getLifeValue()<=10 && getLifeValue()>=5){
			canvas.drawBitmap(Image, x, y,null);
			Move();
		}
		else if(getLifeValue()<5&& getLifeValue()>=1){
			canvas.drawBitmap(ImageHit, x, y,null);
			Move();
		}
		else {
			switch (num/3) {
			case 0:
				canvas.drawBitmap(Image1, x, y,null);
				break;
			case 1:
				canvas.drawBitmap(Image2, x, y,null);
				break;
			case 2:
				canvas.drawBitmap(Image3, x, y,null);
				break;
			case 3:
				canvas.drawBitmap(Image4, x, y,null);
				break;
			default:
				break;
			}
			num++;
		}
	}
	@Override
	public void Move() {
		// TODO Auto-generated method stub
		y+=speed;
	}
	@Override
	public int check(){
		if(num>=12){
			return 1;
		}
		if(y>Height){
			return 2;
		}
		return 0;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
		act.playSound(4, 0);
	}
	
}
敵機Boss-EmBoss
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import com.swust.gamedeom.R;

public class EmBoss extends Things{

	private float Height;
	private float Width;
	private int num;
	
	private Bitmap Image1;
	private Bitmap Image2;
	private Bitmap Image3;
	private Bitmap Image4;
	private Bitmap Image5;
	private Bitmap Image6;
	private Bitmap ImageHit;
	
	public EmBoss(Resources r,float SWidth,float SHeight){
		this.res = r;
		Image = BitmapFactory.decodeResource(r, R.drawable.enemy3_n1);
		ImageHit = BitmapFactory.decodeResource(r, R.drawable.enemy3_hit);
		width = Image.getWidth();
		hight = Image.getHeight();
		setX((float)(Math.random()*(SWidth-width)));
		setY(-hight);
		SetSpeed(2);
		this.Height = SHeight;
		this.Width = SWidth;
		setLifeValue(60);
		Init();
		setSorces(300);
	}
	public void Init(){
		Image1 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down1);
		Image2 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down2);
		Image3 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down3);
		Image4 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down4);
		Image5 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down5);
		Image6 = BitmapFactory.decodeResource(res, R.drawable.enemy3_down6);
		num=0;
	}
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(getLifeValue()<=60 && getLifeValue()>=30){
			canvas.drawBitmap(Image, x, y,null);
			Move();
		}
		else if(getLifeValue()<30&& getLifeValue()>=1){
			canvas.drawBitmap(ImageHit, x, y,null);
			Move();
		}
		else {
			switch (num/3) {
			case 0:
				canvas.drawBitmap(Image1, x, y,null);
				break;
			case 1:
				canvas.drawBitmap(Image2, x, y,null);
				break;
			case 2:
				canvas.drawBitmap(Image3, x, y,null);
				break;
			case 3:
				canvas.drawBitmap(Image4, x, y,null);
				break;
			case 4:
				canvas.drawBitmap(Image5, x, y,null);
				break;
			case 5:
				canvas.drawBitmap(Image6, x, y,null);
				break;
			default:
				break;
			}
			num++;
		}
	}
	@Override
	public void Move() {
		// TODO Auto-generated method stub 
		 if(y>=(Height/2-hight)){
			y-=speed;
		}
		 else{
			 y+=speed;
		 }
	}
	@Override
	public int check(){
		if(num>=18){
			return 1;
		}
		return 0;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
		act.playSound(5, 0);
	}
}
アイテム→Ufo
package com.swust.gamedeom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Ufo extends Things{
	private Bitmap Image2;
	private float Height;
	private int  UfoFlag = 0;
	
	public int getUfoFlag() {
		return UfoFlag;
	}
	public void setUfoFlag(int ufoFlag) {
		UfoFlag = ufoFlag;
	}
	public Ufo(Resources r,int flag,float SWidth,float SHeight){
			res = r;
			Image = BitmapFactory.decodeResource(res, R.drawable.ufo1);
			Image2 = BitmapFactory.decodeResource(res, R.drawable.ufo2);
			width = Image.getWidth();
			hight = Image.getHeight();
			Height = SHeight;
			SetSpeed(4);//10
			setUfoFlag(flag);
			setLifeValue(1);
			if(UfoFlag ==1){
				setX(SWidth-width-50);
			}
			else if(UfoFlag == 2){
				setX(50);
			}
	}
	public int check(){
		if(getLifeValue()==0){
			return 1;
		}
		if(y>Height){
			return 2;
		}
		return 0;
	}
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(UfoFlag ==1){
			canvas.drawBitmap(Image, x, y,null);
		}
		else if(UfoFlag ==2){
			canvas.drawBitmap(Image2, x, y,null);
		}
		Move();
	}

	@Override
	public void Move() {
		// TODO Auto-generated method stub
		y+=speed;
	}
	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
		act.playSound(8, 0);
	}

}
弾丸---Bullet
package com.swust.gamedeom;

import java.util.ArrayList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;

public class Bullet extends Things{
	
	private Bitmap Image2;
	private Bitmap Image3;
	private int SuperBullet;

	public void setSuperBullet(int superBullet) {
		SuperBullet = superBullet;
	}

	public Bullet(Resources r){
		res = r;
		Image = BitmapFactory.decodeResource(res, R.drawable.bullet1);
		Image2 = BitmapFactory.decodeResource(res, R.drawable.bullet2);
		Image3 = BitmapFactory.decodeResource(res, R.drawable.bomb);
		width = Image.getWidth();
		hight = Image.getHeight();
		SetSpeed(60);
		setLifeValue(1);
		SuperBullet = 0;
	}
	
	@Override
	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(SuperBullet == 0){
			canvas.drawBitmap(Image, x, y,null);
		}
		else if(SuperBullet == 1){
			canvas.drawBitmap(Image2, x, y,null);
		}
		else if(SuperBullet ==2){
			canvas.drawBitmap(Image3, x, y,null);
		}
		Move();
	}
	@Override
	public void Move() {
		// TODO Auto-generated method stub
		y-=speed;
	}
	@Override
	public int check() {
		if(getLifeValue()==0){
			return 1;
		}
		if(this.y<0){
			return 2;
		}
		return 0;
	}
	/**
	 *           
	 * @param EmPlanelist      
	 * @param act      
	 */
	public void hitenemy( ArrayListEmPlanelist,MainActivity act){
		RectF rect1 = this.getrectf();
		RectF rect2;
		for (int i = 0; i < EmPlanelist.size(); i++) {
			 Things emPlane = EmPlanelist.get(i);
			 rect2 = emPlane.getrectf();
			if(rect1.intersect(rect2)&& emPlane.getLifeValue()!=0){
				this.lifeReduce();
				emPlane.lifeReduce();
			}
		}
	}

	@Override
	public void playsound(MainActivity act) {
		// TODO Auto-generated method stub
	}
}
簡単な工場モデルの使用
package com.swust.gamedeom;

import android.content.res.Resources;

public class EmPlaneFactory {
	
	private EmPlaneFactory(){
		
	}
	public static Things getEmPlane(int x,Resources r,Float SWidth,float SHeight){
		Things emPlane = null;
		switch (x) {
		case 1:
			emPlane = new EmptyPlane(r,SWidth,SHeight);
			break;
		case 2:
			emPlane = new EmBigPlane(r,SWidth,SHeight);
			break;
		case 3:
			emPlane = new EmBoss(r,SWidth,SHeight);
			break;
		}
		return emPlane;
	}	
}
メインプログラムとオーディオの読み込み
package com.swust.gamedeom;

import java.io.IOException;
import java.util.HashMap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;


@SuppressLint("UseSparseArrays")
public class MainActivity extends Activity implements Runnable {
	
    private MySuifaceView view;
    private MediaPlayer mMediaPlayer;  
	private SoundPool sp;
    private HashMap spMap;
    public boolean Loadflag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE); 		
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  //      
	DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        
        Paint paint = new Paint();
	Typeface font = Typeface.create(Typeface.SANS_SERIF,Typeface.BOLD);
	paint.setTypeface(font);
	paint.setColor(0xff949694);
	paint.setTextSize(55);
        
        float width = (float)metric.widthPixels;
        float height = (float) metric.heightPixels;
        Loadflag = false;
		view = new MySuifaceView(this,width,height,this,paint);
        setContentView(view); 
        new Thread(this).start();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void InitSound() {
    	
    	mMediaPlayer = MediaPlayer.create(this, R.raw.game_music); 
        sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        spMap = new HashMap();
        spMap.put(2, sp.load(this, R.raw.bullet, 1));
        spMap.put(3, sp.load(this, R.raw.enemy1_down,1));
        spMap.put(4, sp.load(this, R.raw.enemy2_down,1));
        spMap.put(5, sp.load(this, R.raw.enemy3_down,1));
        spMap.put(6, sp.load(this, R.raw.use_bomb,1));
        spMap.put(7, sp.load(this, R.raw.game_over,1));
        spMap.put(8, sp.load(this, R.raw.get_bomb,1));
        Loadflag = true;
    }

    public void playSound(int sound, int number) {
    	
    	if(sound==1){
    	     if (!mMediaPlayer.isPlaying()) {  
                mMediaPlayer.start();  
            }  
    	}
    	else{
    		AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    		float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    		float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    		float volumnRatio = volumnCurrent / audioMaxVolumn;
    		sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number,  1f);
    	}  
    }
    public void stopSound(){
    	
    	if (mMediaPlayer.isPlaying()){  
            mMediaPlayer.pause();  
        }  
    }
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	setContentView( view );
    	super.onResume();
    }

	@Override
	public void run() {
		// TODO Auto-generated method stub
		 InitSound();
		 try {
			mMediaPlayer.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	  
}
ここではスレッドを通してオーディオを単独で読み込み、オーディオプールを使用します.
六:Suifaceview類(コア)
package com.swust.gamedeom;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MySuifaceView extends SurfaceView implements Callback, Runnable {

	private ArrayList bulletlist = new ArrayList();
	private ArrayList EmPlanelist = new ArrayList();
	
	private ArrayList bomblist = new ArrayList();
	private Ufo ufo_double;
	private Ufo ufo_super;
	private Plane plane;
	private SurfaceHolder mSurfaceHolder = null;
	private boolean mbloop = false;
	private BackGround bk;
	private MainActivity activity;
	
	private Point point = new Point();
	private Rect rect;
	private boolean canDrag = false;
	private int offsetX = 0, offsetY = 0;
	private int Count = 0;
	private int bossCount = 0;
	private float SWidth;
	private float SHeight;
	private int Score;
	private int statue;
	private Rect rectbtn;
	private Canvas canvas=null;
	private int Emcreat;
	private int EmBicreat;
	private int EmBosscreat;
	
	private Rect rectMusic;
	private Rect rectPassue;
	private Rect rectAgain;
	private Rect rectContinue;
	private Rect rectOver;
	
	private boolean bullet_double;
	private boolean bullet_super;
	private boolean bombScreen;
	private int bulletTime;
	private int bulletcreat;
	private Rect rectbomb;
	private boolean isPlay;
	
	@TargetApi(19)
	@SuppressLint("NewApi")
	public MySuifaceView(Context context, float width, float height,MainActivity act,Paint paint) {
		super(context);
		// TODO Auto-generated constructor stub
		//       
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		this.setFocusable(true);
		this.setKeepScreenOn(true);
		this.activity = act;
		Init(width,height);
		bk = new BackGround(getResources(),width, height,paint);
		SWidth = width;
		SHeight = height;
		isPlay = true;
		statue =0;
		
		rectbtn = new Rect((int)(width-300)/2,(int)(height-200),(int)(width/2+150),(int)(height-150));
		rectPassue = new Rect(0,0,150,150);
		rectContinue = new Rect((int)(width-300)/2,(int)(height/2-150),(int)(width/2+150),(int)(height/2-90));
		rectAgain =  new Rect((int)(width-300)/2,(int)(height/2),(int)(width/2+150),(int)(height/2+60));
		rectOver = new Rect((int)(width-300)/2,(int)(height/2+150),(int)(width/2+150),(int)(height/2+210));
		rectbomb = new Rect(20,(int)(height-100),160,(int)(height));
		rectMusic = new Rect((int)width-50,0,(int)width,50);
	}
	public void Init(float width, float height){
		plane = new com.swust.gamedeom.Plane(getResources(), width, height);
		rect = new Rect((int) plane.getX(), (int) plane.getY(),
				(int) plane.getR(), (int) plane.getB());
		
		bulletcreat = 7;
		Emcreat = 15;
		EmBicreat = 150;
		EmBosscreat = 3379;
		
		Score = 0;
		Count = 0;
		bullet_double = false;
		bullet_super = false;
		bulletTime = 0;
		bombScreen = false;
		bossCount = 0;
		
		
		bulletlist.clear();
		EmPlanelist.clear();
		bomblist.clear();
		ufo_double = null;
		ufo_super = null;
		
		
	}
	public void DrawBitmap(){
		bk.setAct(activity);
		bk.setSources(Score);
		bk.setStatue(statue,isPlay);
		bk.Draw(canvas);
		if(statue == 1){
			plane.Draw(canvas);
			if(plane.check()!=0){
				statue = 3;
				activity.stopSound();
			}
			plane.hitenemy(EmPlanelist,activity);
			if(ufo_double!=null){
				ufo_double.Draw(canvas);
				bullet_double = plane.GetUfo(ufo_double,activity);
				if(ufo_double.check()!=0){
					ufo_double=null;
				}
			}
			if(ufo_super!=null){
				ufo_super.Draw(canvas);
				bullet_super = plane.GetUfo(ufo_super,activity);
				if(ufo_super.check()!=0){
					ufo_super=null;
				}
			}
			for (int i = 0; i < bulletlist.size(); i++) {
				Bullet bullet = bulletlist.get(i);
				bullet.Draw(canvas);
				bullet.hitenemy(EmPlanelist,activity);
				if (bullet.check()!=0) {
					bulletlist.remove(bullet);
					i--;
				}
			}
			for (int i = 0; i < EmPlanelist.size(); i++) {
				Things emPlane = EmPlanelist.get(i);
				if(emPlane.check()==1){
					Score+=emPlane.getSorces();
					emPlane.playsound(activity);
					EmPlanelist.remove(emPlane);
					i--;
				}
				else if(emPlane.check()==2){
					EmPlanelist.remove(emPlane);
					i--;
				}
				else{
					emPlane.Draw(canvas);
				}
			}
			for (int m = 0; m < bomblist.size(); m++) {
					Bomb bomb = bomblist.get(m);
					if(bombScreen){
						bomb.lifeReduce();
						ScreenBomb();
						bombScreen = false;
					}
					if(bomb.check()!=0){
						bomblist.remove(bomb);
						m--;
					}
					else{
						bomb.setBombnum(bomblist.size());
						bomb.Draw(canvas);
					}
			}
		}
	}
	public  void Draw() {
		try {
			 canvas = mSurfaceHolder.lockCanvas();
			 if(canvas!=null){
				 DrawBitmap();
			 }
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			if(canvas!=null)
				mSurfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		mbloop = true;
		new Thread(this).start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		mbloop = false;
		activity.stopSound();
		isPlay = false;
		statue =2;
	}
	
	@Override
	public void run() {  
        // TODO Auto-generated method stub  
        int SLEEP_TIME=15;  
        while (mbloop) {  
            long start=System.currentTimeMillis();  
            Draw();
            if(statue == 1){
            	Count++;
            	//Adjust();
            	AddEmBigPlane();
            	Addbuilt();
            	AddEmplane();
            	AddEmBoss();
            	AddUfo();
            	Addbomb();
            	if(bullet_double){
            		checktime();
            	}
            }
            long end=System.currentTimeMillis();    
            if((end-start)0){
					bombScreen = true;
				}
			}
			if(statue == 2){
				if(rectContinue.contains(point.x,point.y)){
					statue=1;
				}
				else if(rectAgain.contains(point.x,point.y)){
					Init(SWidth, SHeight);
					statue=1;	
				}
				else if(rectOver.contains(point.x,point.y)){
					activity.finish();
				}
				else if(rectMusic.contains(point.x, point.y)){
					if(isPlay){
						activity.stopSound();
						isPlay = false;
					}
					else{
						activity.playSound(1, -1);
						isPlay = true;
					}
				}
			}
			if (rect.contains(point.x, point.y)) {
				canDrag = true;
				offsetX = point.x - rect.left;
				offsetY = point.y - rect.top;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(canDrag){
			rect.left = (int) event.getX() - offsetX;
			rect.top = (int) event.getY() - offsetY;
			rect.right = rect.left + (int) plane.getWidth();
			rect.bottom = rect.top + (int) plane.getHight();
			if (rect.left < 0) {
				rect.left = 0;
				rect.right = rect.left + (int) plane.getWidth();
			}
			if (rect.right > getMeasuredWidth()) {
				rect.right = getMeasuredWidth();
				rect.left = rect.right - (int) plane.getWidth();
			}
			if (rect.top < 0) {
				rect.top = 0;
				rect.bottom = rect.top + (int) plane.getHight();
			}
			if (rect.bottom > getMeasuredHeight()) {
				rect.bottom = getMeasuredHeight();
				rect.top = rect.bottom - (int) plane.getHight();
			}
			plane.setX((float) rect.left);
			plane.setY((float) rect.top);
			break;
			}
		case MotionEvent.ACTION_UP:
			canDrag = false;
			break;
		default:
			break;
		}
		return true;
	}

}
コアはDrawBitmap()関数であり、スレッドは15 msごとに一度、ドラフトBitmap()関数を呼び出して各オブジェクトを描画すると、ゲームの効果が現れます.
七:まとめ
全体としては、2週間の時間を使って、何もできませんでした.小さなゲームができるようになりました.いろいろなことを学びました.Surfaceviewの認識はもっと深くなりました.