主要学习内容:
1、图形编程
2、自定义View类
3、FontMmetrics
4、单击触摸事件
5、碰撞检测
6、可用数据计算
图形编程基本概念:
1、颜色对象
Color 安卓系统中的颜色的表示方法
(1)、int color = Color.blue; //纯色
(2)、int color = Color.argb(188,255,255,255);//自定义颜色
(3)、在xml文件当中定义颜色
2、画笔对象
Paint 该类的对象用于控制画笔的风格和颜色等方面的信息
(1)、paint.setColor(Color.blue);
3、canvas 该类代表一块“画布”,可以在“画布”上绘制字符,图形和图片
(1)、canvas.drawcircle(300,400,100,paint);
自定义View的基本实现方法:
(1)、定义一个类,继承View
(2)、复写View的onDraw函数
(3)、在onDraw当中使用canvas和paint对象绘制图形
Paint的设置方法:
1、setAntiAlias:设置画笔的锯齿效果
2、setARGB:设置画笔的argb对象
3、setTextSize:设置字体尺寸
4、setColor:设置画笔颜色
5、setAlpha:设置透明度值
6、setStyle:设置画笔风格,空心或实心
7、getColor:得到画笔颜色
8、getApha:得到画笔的透明度值
public boolean onTouchEvent(MotionEvent event){
//获得事件的种类
event.getAction();
//获取点击的坐标
event.getX();
event.getY();
}
直接上代码
总共四个类
一、ShuduView.java 游戏界面构画
1 package myview; 2 3 import xqx.shudu.Game; 4 import xqx.shudu.R; 5 import xqx.shudu.SelectDialog; 6 7 import android.app.AlertDialog; 8 import android.app.AlertDialog.Builder; 9 import android.content.Context; 10 import android.content.DialogInterface; 11 import android.graphics.Canvas; 12 import android.graphics.Color; 13 import android.graphics.Paint; 14 import android.graphics.Paint.Align; 15 import android.graphics.Paint.FontMetrics; 16 import android.graphics.Paint.Style; 17 import android.view.LayoutInflater; 18 import android.view.MotionEvent; 19 import android.view.View; 20 import android.widget.TextView; 21 import android.widget.Toast; 22 23 public class ShuduView extends View{ 24 //记录单元格的高度和宽度 25 private float width; 26 private float height; 27 private Game game = new Game(); 28 int selectx; 29 int selecty; 30 public ShuduView(Context context) { 31 super(context); 32 // TODO Auto-generated constructor stub 33 } 34 @Override 35 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 36 // TODO Auto-generated method stub 37 //计算当前单元格的宽度和高度 38 this.width = w/9f;//每一格的宽度 39 this.height = h/9f;//每一格的高度 40 super.onSizeChanged(w, h, oldw, oldh); 41 } 42 @Override 43 protected void onDraw(Canvas canvas) { 44 // TODO Auto-generated method stub 45 //1、绘制背景 46 Paint bgpaint = new Paint(); 47 bgpaint.setColor(Color.GRAY); 48 //绘制背景色 49 canvas.drawRect(0, 0, getWidth(), getHeight(), bgpaint); 50 51 Paint drakpaint = new Paint(); 52 drakpaint.setColor(Color.WHITE); 53 54 Paint whitepaint = new Paint(); 55 whitepaint.setColor(Color.BLACK); 56 57 for (int i = 0; i < 9; i++) { 58 //绘制横向的单元格线 59 canvas.drawLine(0, i*height, getWidth(), i*height, whitepaint); 60 canvas.drawLine(0, i*height+1, getWidth(), i*height+1, whitepaint); 61 //绘制纵向的单元格的线 62 canvas.drawLine(i*width, 0, i*width, getHeight(), whitepaint); 63 canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), whitepaint); 64 } 65 //绘制横竖各三条分割线 66 for (int i = 0; i < 9; i++) { 67 if(i%3!=0) 68 { 69 continue; 70 } 71 //绘制横向的单元格线 72 canvas.drawLine(0, i*height, getWidth(), i*height, drakpaint); 73 canvas.drawLine(0, i*height+1, getWidth(), i*height+1, drakpaint); 74 //绘制纵向的单元格的线 75 canvas.drawLine(i*width, 0, i*width, getHeight(), drakpaint); 76 canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), drakpaint); 77 } 78 //绘制数字 79 Paint numberpaint = new Paint(); 80 numberpaint.setColor(Color.BLACK); 81 numberpaint.setStyle(Style.STROKE); 82 numberpaint.setTextSize((float) (height*0.75)); 83 numberpaint.setTextAlign(Align.CENTER);//居中对齐 84 85 FontMetrics fm = numberpaint.getFontMetrics(); 86 float x = width/2; 87 float y = height/2-(fm.ascent-fm.descent)/2; 88 // 计算文字高度 89 float fontHeight = fm.bottom - fm.top; 90 // 计算文字baseline 91 float textBaseY = height - (height - fontHeight) / 2 - fm.bottom; 92 //canvas.drawText("1", 3*width+x, textBaseY, numberpaint); 93 //绘制数字 94 for(int i=0;i<9;i++) 95 { 96 for(int j=0; j<9;j++) 97 { 98 canvas.drawText(game.getTitlStringe(i,j), i*width+x, j*height+textBaseY, numberpaint); 99 }100 }101 super.onDraw(canvas);102 }103 //触摸事件104 @Override105 public boolean onTouchEvent(MotionEvent event) {106 // TODO Auto-generated method stub107 if(event.getAction()!=MotionEvent.ACTION_DOWN)108 {109 return super.onTouchEvent(event);110 }111 112 //得到点击位置的x,y坐标113 selectx = (int) (event.getX()/width);114 selecty = (int) (event.getY()/height);115 116 int used[] = game.getused(selectx,selecty);117 StringBuffer strbuf = new StringBuffer();118 for(int i=0;i
二、Game.java 用于处理数独数据
1 package xqx.shudu; 2 3 public class Game { 4 //数独数据初始化 5 private final String str = "360000000" + 6 "004230800" + 7 "000004200" + 8 "070460003" + 9 "820000014" + 10 "500013020" + 11 "001900000" + 12 "007048300" + 13 "000000045"; 14 private int shuduku[]=new int[9*9]; 15 //用来存储每个单元格不可填写的数字 16 //1维 x坐标 2维 y坐标 3维 不可填写数字 17 private int used[][][]=new int[9][9][]; 18 public Game(){ 19 shuduku = fromPuzzleString(str); 20 calAllused(); 21 } 22 23 //根据字符串数据生成整型数组 24 public int[] fromPuzzleString(String str2) { 25 // TODO Auto-generated method stub 26 int shudu[] = new int[str2.length()]; 27 for(int i=0;i
三、SelectDialog.java 用于设置填写数字的对话框
1 package xqx.shudu; 2 3 import myview.ShuduView; 4 import android.app.Dialog; 5 import android.content.Context; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Toast; 9 10 public class SelectDialog extends Dialog{11 int q;12 //当dialog第一次显示时会调用其onCreate方法13 //用来存放对话框中按钮对象14 private ShuduView shuduview;15 private View key[] = new View[9];16 private int used[];17 public SelectDialog(Context context, int used[],ShuduView shuduview) {18 19 20 super(context);21 this.used=used;22 this.shuduview = shuduview;23 // TODO Auto-generated constructor stub24 }25 26 @Override27 protected void onCreate(Bundle savedInstanceState) {28 // TODO Auto-generated method stub29 super.onCreate(savedInstanceState);30 setTitle("选择填入的数字");31 setContentView(R.layout.shudu_diolag);//设置布局文件32 findView();33 34 for(int i=0;i
四、MainActivty.java 在主类中调用MyView对象
1 package xqx.shudu; 2 import myview.*; 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 public class MainActivity extends Activity { 8 9 @Override10 protected void onCreate(Bundle savedInstanceState) {11 super.onCreate(savedInstanceState);12 setContentView(new ShuduView(this));13 // setContentView(R.layout.activity_main);14 }15 16 17 @Override18 public boolean onCreateOptionsMenu(Menu menu) {19 // Inflate the menu; this adds items to the action bar if it is present.20 getMenuInflater().inflate(R.menu.main, menu);21 return true;22 }23 24 }
五:布局文件
1 28 9 10 12 14 16 1718 20 22 24 2526 28 30 32 33 3439 40
效果图: