我们首先把数独游戏的布局,自定义View和初始化数字给完成;
我们把画笔的颜色写进color.xml里
-
color.xml(画笔颜色)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#ffe6f0ff</color>
<color name="hilite">#ffffffff</color>
<color name="light">#64c6d4ef</color>
<color name="dark">#6456648f</color>
</resources>
-
MainActivity.java(引用自定义View布局)
/**
* 数独游戏开发
* @author dandan
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyView(this));
}
}
-
MyView.java(自定义View)
/**
* 数独游戏开发
* @author dandan
*
*/
public class MyView extends View{
private float widht,height;
private Game game = new Game();
//构造方法
public MyView(Context context) {
super(context);
}
//得到改变的视图宽和高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.widht=w/9f;
this.height=h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
//画视图,⑨宫格
@Override
protected void onDraw(Canvas canvas) {
//用于生成背景色的画笔(共4种颜色的画笔)
Paint backgroundpaint = new Paint();
//设置画笔颜色
backgroundpaint.setColor(getResources().getColor(R.color.background));
//画出背景
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundpaint);
Paint darkpaint = new Paint();
darkpaint.setColor(getResources().getColor(R.color.dark));
Paint lightpaint = new Paint();
lightpaint.setColor(getResources().getColor(R.color.light));
Paint hilitepaint = new Paint();
hilitepaint.setColor(getResources().getColor(R.color.hilite));
//下面开始画线
for(int i=0;i<9;i++){
//画横线
canvas.drawLine(0, i*height, getWidth(), i*height, lightpaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitepaint);
//画竖线
canvas.drawLine(i*widht,0,i*widht, getHeight(), lightpaint);
canvas.drawLine(i*widht+1,0,i*widht+1, getHeight(), hilitepaint);
}
//画外面的最清楚的4个深色的线
for(int i=0;i<9;i++){
if(i%3!=0){continue;}
//画横线
canvas.drawLine(0, i*height, getWidth(), i*height, darkpaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitepaint);
//画竖线
canvas.drawLine(i*widht,0,i*widht, getHeight(), darkpaint);
canvas.drawLine(i*widht+1,0,i*widht+1, getHeight(), hilitepaint);
}
//框框里添加文本
Paint numpaint = new Paint();
numpaint.setColor(Color.BLACK);
numpaint.setStyle(Paint.Style.STROKE);
numpaint.setTextAlign(Paint.Align.CENTER);
numpaint.setTextSize(height*0.75f);
//數字的位置
FontMetrics fm = numpaint.getFontMetrics();
float x = widht/2;
float y = height/2-(fm.ascent+fm.descent)/2;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
canvas.drawText(game.getStringTile(i,j), i*widht+x,j*height+y, numpaint);
}
}
super.onDraw(canvas);
}
}
-
Game.java(游戏初始化数字)
/**
* 数独游戏开发
* @author dandan
*
*/
public class Game {
//數獨初始化數據
private final String data = "360000000004230800000004200"+
"070460003820000014500013020"+
"001900000007048300000000045";
private int[] shuduku = new int[9*9];
//根據一個字符串數據,生成一個整型數組
protected int[] fromString(String src){
int[] shudu = new int[src.length()];
for(int i=0;i<src.length();i++){
shudu[i] = src.charAt(i)-'0';
}
return shudu;
}
public Game(){
shuduku = fromString(data);
}
//根據九宮格的坐標,返回坐標所對應的數字
private int getTile(int x,int y){
return shuduku[y*9+x];
}
//得到字符串
public String getStringTile(int x,int y){
int v = getTile(x,y);
if(v==0){
return "";
}
return String.valueOf(v);
}
}
-
演示:
-
源码地址:
链接:http://pan.baidu.com/s/1cI89rw 密码:ss73