标签:
经过了前两天的设置后,我们就可以再View类中,进行游戏的布置,和进行操作了。
package com.mars.demo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class MyView extends View { //定义单元格的宽和高 public float width; public float height; private int selectX; private int selectY; public Game game = new Game(); public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub this.width = w/9f; this.height = h/9f; super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { //定义画笔对象-->绘制背景 Paint paint = new Paint(); //设置画笔的颜色 paint.setColor(getResources().getColor(R.color.shudu_background)); //绘制手机屏幕背景色 canvas.drawRect(0, 0, getWidth(), getHeight(), paint); //定义画笔对象-->绘制格子线条 Paint darkPaint = new Paint(); darkPaint.setColor(getResources().getColor(R.color.shudu_dark)); Paint hilitePaint = new Paint(); hilitePaint.setColor(getResources().getColor(R.color.shudu_hilite)); Paint lightPaint = new Paint(); lightPaint.setColor(getResources().getColor(R.color.shudu_light)); //绘制九宫格 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*width, 0, i*width, getHeight(), lightPaint); canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hilitePaint); } //这个for循环是绘制四根醒目的线 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*width, 0, i*width, getHeight(), darkPaint); canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hilitePaint); } //绘制数字 Paint numberPaint = new Paint(); //设置文字的一系列属性 numberPaint.setColor(Color.BLACK); numberPaint.setStyle(Paint.Style.STROKE); numberPaint.setTextSize(height*0.75f); numberPaint.setTextAlign(Paint.Align.CENTER); FontMetrics fontMetrics = numberPaint.getFontMetrics(); float x = width/2; float y = height/2 - (fontMetrics.ascent+fontMetrics.descent)/2; for(int i=0; i<9; i++) for(int j=0; j<9; j++) { canvas.drawText(game.getTitleString(i, j), i*width + x, j*height+y, numberPaint); } super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() != MotionEvent.ACTION_DOWN) { return super.onTouchEvent(event); } selectX = (int)(event.getX()/width); selectY = (int)(event.getY()/height); int[] used = game.getUsedTile(selectX, selectY); StringBuffer sb = new StringBuffer(); for(int i = 0; i<used.length; i++) { sb.append(used[i]); } Log.i("TAG", sb.toString()); KeyDialog keyDialog = new KeyDialog(this.getContext(), used, this); keyDialog.show(); /*//定义一个layoutInflate对象,转载view文件 LayoutInflater layoutInflater = LayoutInflater.from(this.getContext()); View view = layoutInflater.inflate(R.layout.dialog, null); TextView textView = (TextView) view.findViewById(R.id.usedTextId); textView.setText(sb.toString()); //创建对话框,弹出view AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); builder.setView(view); AlertDialog dialog = builder.create(); dialog.show();*/ return true; } //View类接受KeyDialog传递过来的数据,调用业务逻辑Game类,进行处理 public void setSelectTile(int tile) { Log.i("TAG", game.setTileIfValid(selectX, selectY, tile)+""+tile); if(game.setTileIfValid(selectX,selectY,tile)) { invalidate();//重新绘制View对象 } } }
在主类中调用MyView对象。
package com.mars.demo; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); setContentView(new MyView(this)); } }
通过上面数独游戏的学习,我了解了Paint类,Canvas类。我学习了一些绘制图形的基本知识,进一步巩固了一些android的基本知识。从中,我发现,作为一个程序员,一定要不断的去敲代码,勇于去实践,程序时敲出来的。很多时候,我们看别人写程序,感觉逻辑都很清晰,思路也不紊乱,但是,真正当自己去实践的时候,你会发现,其实并不是那么容易。当你出现问题的时候,你得尝试去调试,这样,我们才能不断成长。
程序员都是在调试Bug,调试错误中成长起来的。这是我非常赞同的一句话。在此,给自己一句勉励的话,希望自己在今后的学习日子中,不怕困难,用于去尝试。
标签:
原文地址:http://www.cnblogs.com/kasion/p/4838886.html