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

2024-05-04 10:08

本文主要是介绍【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

这篇关于【Android基础入门〖7〗】SurfaceView坦克大战之世界地图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/958945

相关文章

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat