数独游戏(2)之单点触摸事件,碰撞检测,计算出不可用数据
1)单点触摸事件
就是手触摸屏幕时,根据操作的行为来激发某事件
需要重写view父类的 public boolean onTouchEvent(MotionEvent event) 方法
package com.soduku;
import com.soduku.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.MotionEvent;
import android.view.View;
public class ShuduView extends View {
//单元格的宽度和高度
private float width ;
private float height ;
private Game game = new Game();
public ShuduView(Context context)
{
super(context);
}
//w:整个 veiw 的宽度; h:整个 veiw 的高度
@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
public void onDraw(Canvas canvas) {
Paint backgroundPaint = new Paint();
//从配置文件中 获取颜色值
backgroundPaint.setColor(this.getResources().getColor(R.color.shudu_background));
//画出整个手机屏幕的 的背景色
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backgroundPaint);
//----------------------------------------
Paint darkPaint = new Paint();
darkPaint.setColor(this.getResources().getColor(R.color.shudu_dark));
Paint hilitePaint = new Paint();
hilitePaint.setColor(this.getResources().getColor(R.color.shudu_hilite));
Paint lightPaint = new Paint();
lightPaint.setColor(this.getResources().getColor(R.color.shudu_light));
//画九宫格里面的 横线,纵线,每次画出的线要想达到