android数独游戏源码
照着mars的视频教程学的
[java]
package com.example.android;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
@SuppressLint("DrawAllocation")
public class myView extends View{
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//方格长宽
float width;
float height;
//选定的坐标
int selectX;
int selectY;
myGame mGame=new myGame();
//绘图函数
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画背景
Paint bgPaint=new Paint();
bgPaint.setColor(getResources().getColor(R.color.bg));
canvas.drawRect(0,0,getWidth(),getHeight()*2/3f, bgPaint);
//线条画笔
Paint darkPaint =new Paint();
darkPaint.setColor(getResources().getColor(R.color.dark));
Paint hilitePaint =new Paint();
hilitePaint.setColor(getResources().getColor(R.color.hilite));
Paint lightPaint =new Paint();
lightPaint.setColor(getResources().getColor(R.color.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);
if(i%3==0){
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(Align.CENTER);
//调节文字居中
FontMetrics fMetrics=numberPaint.getFontMetrics();
float x=width/2;
float y=height/2-(fMetrics.ascent+fMetrics.descent)/2;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
canvas.drawText(mGame.getNumber(i+1, j+1), i*width+x, y+j*height, numberPaint);
}
}
}
//触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
int x=(int)(event.getX()/width);
int y=(int)(event.getY()/height);
int []t=mGame.getUsed(x, y);
selectX=x;
selectY=y;
myDialog mDialog=new myDialog(getContext(), t, this);
mDialog.show();
return super.onTouchEvent(event);
}
//获得屏幕尺寸
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//每一个小格的长宽
this.width=w/9f;
this.height=h/9f*2/3;
}
public void setTitle(int i){
mGame.setTitle(i,selectX,selectY);
invalidate();
}
}
[java]
package com.example.android;
public class myGame {
String data="008309100" +
"900060004" +
"007504800" +
"036000540" +
补充:移动开发 , Android ,