【Android基础入门〖7〗】SurfaceView坦克大战之世界地图

目录(?)[+]

1  自定义 SurfaceView

WarView.java  (世界战场)           

  1. package com.mytank;  
  2. import java.util.Vector;  
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Paint.Style;  
  8. import android.util.AttributeSet;  
  9. import android.view.MotionEvent;  
  10. import android.view.SurfaceHolder;  
  11. import android.view.SurfaceView;  
  12. public class WarView extends SurfaceView implements SurfaceHolder.Callback {  
  13.     SurfaceHolder mholder;  
  14.     boolean run_flag;  
  15.     Canvas canvas;  
  16.     DrawThread thread;  
  17.       
  18.     Vector<Wall>walllist=new Vector<Wall>();  
  19.       
  20.     int row;//行  
  21.     int col;//列  
  22.     int warmap[][];  
  23.       
  24.     float x=10,y=10;  
  25.       
  26.     public WarView(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         mholder=this.getHolder();  
  29.           
  30.         if(mholder!=null)  
  31.             mholder.addCallback(this);  
  32.     }  
  33.       
  34.     //初始化地图  
  35.     public void initmap()  
  36.     {  
  37.         //0 空, 1 墙  
  38.         for(int i=0;i<col;i++)  
  39.         {  
  40.             warmap[0][i]=1;  
  41.             warmap[1][i]=1;  
  42.             Wall wall=new Wall(0,i);  
  43.             walllist.add(wall);  
  44.             wall=new Wall(1,i);  
  45.             walllist.add(wall);  
  46.         }  
  47.         for(int i=0;i<10;i++)  
  48.         {  
  49.             warmap[i][10]=1;  
  50.             warmap[i][11]=1;  
  51.             Wall wall=new Wall(i,10);  
  52.             walllist.add(wall);  
  53.             wall=new Wall(i,11);  
  54.             walllist.add(wall);  
  55.         }  
  56.     }  
  57.     @Override  
  58.     public void surfaceCreated(SurfaceHolder holder) {  
  59.         run_flag=true;  
  60.         row=this.getHeight()/Common.CELL_LENGTH;  
  61.         col=this.getWidth()/Common.CELL_LENGTH;  
  62.         warmap=new int[row][col];  
  63.         thread=new DrawThread();  
  64.         initmap();  
  65.         thread.start();  
  66.     }  
  67.       
  68.     @Override  
  69.     public boolean onTouchEvent(MotionEvent event) {  
  70.         // TODO 自动生成的方法存根  
  71.         x=event.getX();  
  72.         y=event.getY();  
  73.           
  74.           
  75.         /*  备注:  一定要将return super.onTouchEvent(event)修改为return true 
  76.          *  原因:  
  77.          *        1: 父类的onTouchEvent(event)方法没有做任何处理,并且返回了false。; 
  78.          *        2: 一旦返回false,将不再收到 MotionEvent.ACTION_MOVE 及 MotionEvent.ACTION_UP 事件; 
  79.          */  
  80.         return true;  
  81.     }  
  82.    
  83.     //自定义 界面绘制 线程  
  84.     class DrawThread extends Thread  
  85.     {  
  86.         @Override  
  87.         public void run() {  
  88.               
  89.             //画刷  
  90.             Paint paint=new Paint();  
  91.             paint.setStyle(Style.FILL);  
  92.             paint.setColor(Color.rgb(25500));    //红绿蓝  
  93.               
  94.             Paint bkpaint=new Paint();  
  95.             bkpaint.setStyle(Style.FILL);            //设置填充方式  
  96.             bkpaint.setColor(Color.rgb(2,17,17));    //红绿蓝  
  97.               
  98.             while(run_flag)  
  99.             {  
  100.                 //锁定画布  
  101.                 canvas=mholder.lockCanvas();  
  102.                 if (canvas == null)  
  103.                     continue;  
  104.                   
  105.                 //绘制背景  
  106.                 canvas.drawRect(00, Common.CELL_LENGTH*col, Common.CELL_LENGTH*row, bkpaint);  
  107.                   
  108.                 //绘制手势跟踪  
  109.                 canvas.drawCircle(x, y, 15, paint);  
  110.                   
  111.                 //绘制墙壁  
  112.                 for(int i=0;i<walllist.size();i++)  
  113.                     walllist.get(i).Draw(canvas);  
  114.                   
  115.                 //取消锁定  
  116.                 mholder.unlockCanvasAndPost(canvas);  
  117.             }  
  118.         }  
  119.     }  
  120.       
  121.     @Override  
  122.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  123.     }  
  124.     @Override  
  125.     public void surfaceDestroyed(SurfaceHolder holder) {  
  126.         run_flag=false;        //关闭界面绘制线程  
  127.     }  
  128. }  

2  在布局中使用自定义的 WarView

main_layout.xml

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent">  
  4.     <com.mytank.WarView  
  5.         android:layout_width="match_parent"  
  6.         android:layout_height="match_parent"/>  
  7. </FrameLayout>  

3  封装的 墙块类

Wall.java

  1. package com.mytank;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.graphics.Rect;  
  6. import android.graphics.Paint.Style;  
  7. /** 
  8.  * @author michael.mao 
  9.  * @description 封装的 墙块类,包含自绘制方法 
  10.  */  
  11. public class Wall {  
  12.       
  13.     public int mrow;    //该墙壁所在行  
  14.     public int mcol;  
  15.     Paint inPaint;        //墙壁内边框画刷  
  16.     Paint outPaint;  
  17.     Rect inRect;  
  18.     Rect outRect;  
  19.       
  20.     public Wall(int row, int col)  
  21.     {  
  22.         this.mrow=row;  
  23.         this.mcol=col;  
  24.           
  25.         inPaint=new Paint();  
  26.         inPaint=new Paint();  
  27.         inPaint.setStyle(Style.FILL);  
  28.         inPaint .setColor(Color.rgb(865335));//红绿蓝  
  29.           
  30.         outPaint=new Paint();  
  31.         outPaint=new Paint();  
  32.         outPaint.setStyle(Style.STROKE);  
  33.         outPaint.setColor(Color.rgb(989898));  
  34.           
  35.         inRect=new Rect( mcol*Common.CELL_LENGTH+1,mrow*Common.CELL_LENGTH+1,  
  36.                        ( mcol+1)*Common.CELL_LENGTH-1,(mrow+1)*Common.CELL_LENGTH-1);  
  37.         outRect=new Rect( mcol*Common.CELL_LENGTH,mrow*Common.CELL_LENGTH,  
  38.                         ( mcol+1)*Common.CELL_LENGTH,(mrow+1)*Common.CELL_LENGTH);  
  39.     }  
  40.       
  41.     public void Draw(Canvas canvas)  
  42.     {  
  43.         canvas.drawRect(inRect,inPaint);  
  44.         canvas.drawRect(outRect,outPaint);  
  45.     }  
  46. }  

4  常量值

Common.java

  1. package com.mytank;  
  2. public class Common {  
  3.     public static int CELL_LENGTH=10;    //块宽    战场被分成 M行 N 列, CELL_LENGTH==行高==列宽  
  4. }  

5  主类

MainActivity.java
  1. package com.mytank;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. public class MainActivity extends Activity {  
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main_layout);  
  9.     }  
  10. }  

6    结果展示

 
              
             



转载请注明出处 :)
原:http://blog.csdn.net/mkrcpp/article/details/11646547

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值