import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // setContentView(new MySurfaceview(this)); setContentView(new GameSurface(this)); }}
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.view.SurfaceHolder; import android.view.SurfaceView; import com.example.y.myapplication.R; /** * Created by fly on 2017/5/24. */ public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{ private SurfaceHolder surfaceHolder; private Canvas canvas; private Paint paint; public static int screenWidth; public static int screenHeight; // Menu相关 //菜单类所需的图片 private GameMenu gameMenu; private Bitmap bmpMenuBG;//菜单页面背景图片 private Bitmap bmpLogo;//菜单页面logo private Bitmap bmpButton;//菜单页面Button private Bitmap bmpText;//菜单页面文本 public GameSurface(Context context){ super(context); surfaceHolder=this.getHolder(); surfaceHolder.addCallback(this); paint=new Paint(); paint.setAntiAlias(true); } @Override public void surfaceCreated(SurfaceHolder holder) { screenWidth=this.getWidth(); screenHeight=this.getHeight(); initBitmap(); new Thread(new Runnable() { @Override public void run() { while (true) { myDraw(); } } }).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } private void myDraw() { canvas =surfaceHolder.lockCanvas(); // 调用GameMenu对象画游戏菜单页面 gameMenu.draw(canvas,paint); if (canvas!=null){ surfaceHolder.unlockCanvasAndPost(canvas); } } // 初始化图片方法 private void initBitmap() { //把图片转换成Bitmap格式 bmpMenuBG=BitmapFactory.decodeResource( this.getResources(),R.drawable.mainmenu); bmpLogo=BitmapFactory.decodeResource( this.getResources(),R.drawable.logo); bmpButton=BitmapFactory.decodeResource( this.getResources(),R.drawable.menustart); bmpText=BitmapFactory.decodeResource( this.getResources(),R.drawable.starttext); // 创建对象 gameMenu = new GameMenu(bmpMenuBG, bmpLogo, bmpButton, bmpText); } }import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; /** * Created by fly on 2017/5/24. */ public class GameMenu { private Bitmap bmpMenuBG; private Bitmap bmpLogo; private Bitmap bmpButton; private Bitmap bmpText; private Rect rect; public GameMenu(Bitmap bmpMenuBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){ this.bmpMenuBG=bmpMenuBG; this.bmpLogo=bmpLogo; this.bmpButton=bmpButton; this.bmpText=bmpText; rect = new Rect(0, GameSurface.screenHeight / 3, GameSurface.screenWidth, GameSurface.screenHeight / 3 + GameSurface.screenHeight / 5); } /** * 画菜单页面 * @param canvas * @param paint */ public void draw(Canvas canvas,Paint paint){ // 画背景图 canvas.drawBitmap(bmpMenuBG,0,0,paint); canvas.drawBitmap(bmpLogo,null,rect,paint); int x=GameSurface.screenWidth/2-bmpButton.getWidth()/2; int y=GameSurface.screenHeight/3*2; canvas.drawBitmap(bmpButton,x,y,paint); int z=GameSurface.screenWidth/2-bmpButton.getWidth()/3; int s=GameSurface.screenHeight/3*2+20; canvas.drawBitmap(bmpText,z,s,paint); } }
飞机大战1
最新推荐文章于 2020-01-02 16:42:10 发布