您的位置:首页 > 职场人生

绘制3*3游戏界面,并实现在其上添加字

2011-12-29 23:54 465 查看
绘制3*3游戏界面,并实现在其上添加字

创建ProjActivity类继承Activity类

public class ProjActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyGridView2 mgv = new MyGridView2(this);
getWindow().setContentView(mgv);
//设置View处于触摸模式是获得焦点
mgv.setFocusableInTouchMode(true);
}
}

在Mainfest.xml中

<activity
android:label="@string/app_name"
android:name=".ProjActivity " >

<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

MyGridView2类

public class MyGridView2 extends View{
//定义列数
private int cols = 3;
//x轴的偏移量
private int offset_x;
//y轴的偏移量
private int offset_y;
//小单元格的宽度
private int smallCellSize;
//大单元格的宽度
private int bigCellSize;
//当前的单元格
private Rect curCell = new Rect();
private int x;
private int y;
//保存输入的数据
private int nums[][] = new int[cols][cols];
public MyGridView2(Context context){
super(context);
}

public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode >= 8 && keyCode <= 16){
setNumValue(keyCode - 7);
}
invalidate();//重新再画一次
return super.onKeyDown(keyCode,event);
}

@Override
public boolean onTouchEvent(MotionEvent event){
//获取当前单元格的左上角的坐标
invalidate(curCell);
x = (int)((event.getX() - offset_x) / smallCellSize) * smallCellSize + offset_x;
y = (int)((event.getY() - offset_y) / smallCellSize) * smallCellSize + offset_y;
//修改矩形的尺寸
curCell.set(x,y,x+smallCellSize,y+smallCellSize);
//设置当前单元格为脏数据
invalidate(curCell);//要求重新绘制curCell
return super.onTouchEvent(event);

}

@Override
protected void onDraw(Canvas canvas){
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//获取画布的尺寸
int w,h;
w = getWidth();
h = getHeight();
//计算偏移量
//如果是h>w
if(h>w){
offset_x = 20;
bigCellSize = w - offset_x * 20;
smallCellSize = bigCellSize / cols;
offset_y = (h - bigCellSize) / 2;
}else{
offset_y = 20;
bigCellSize = bigCellSize / cols;
offset_x = (w - bigCellSize) / 2;
}

//画线
//定义线的样式
Paint linePaint = new Paint();
linePaint.setColor(Color.GREEN);
//画水平线
for(int i=0;i<=cols;i++){
canvas.drawLine(offset_x,offsex_y + i * smallCellSize, offset_x + bigCellSize, offset_y + i * smallCellSize, linePaint);
}
for(int i=0;i<=cols;i++){
canvas.drawLine(offset_x + i * smallCellSize, offset_y, offset_x + i * smallCellSize, offset_y + bigCellSize, linePaint);
}

//画当前的单元格
Paint curCellPaint = new Paint();
curCellPaint.setColor(Color.argb(160,255,0,0));
canvas.drawRect(curCell,curCellPaint);

//画数字
Paint textPaint = new Paint();
textPaint.setTextAlign(Paint.Align.CENTER);//放在中间
textPaint.setTextSize((int)(0.75 * smallCellSize));
textPaint.setColor(Color.GREEN);

for(int i=0;i<cols;i++){
for(int j=0;j<cols;j++){
float x = offset_x + j * smallCellSize + smallCellSize / 2;
float_y = offset_y + i * smallCellSize + smallCellSize / 2 - (fm.ascent + fm.descent) / 2;

canvas.drawText(getNumString(nums[i][j]), x,y,textPaint);
}
}

Log.d("ProjActivity","调用onDraw");
}

//把数字转化为字符串
private String getNumString(int num){
if(num == 0){
return "";
}else{
return num + "";
}
}

//设置数组的值
private void setNumValue(int value){
int ax,ay;
ax = (y - offset_y) / smallCellSize;
ay = (x - offset_x) / smallCellSize;
nums[ax][ay] = value;
}
}

绘制3*3游戏界面,并实现在其上添加字

创建ProjActivity类继承Activity类

public class ProjActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyGridView2 mgv = new MyGridView2(this);
getWindow().setContentView(mgv);
//设置View处于触摸模式是获得焦点
mgv.setFocusableInTouchMode(true);
}
}

在Mainfest.xml中

<activity
android:label="@string/app_name"
android:name=".ProjActivity " >

<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

MyGridView2类

public class MyGridView2 extends View{
//定义列数
private int cols = 3;
//x轴的偏移量
private int offset_x;
//y轴的偏移量
private int offset_y;
//小单元格的宽度
private int smallCellSize;
//大单元格的宽度
private int bigCellSize;
//当前的单元格
private Rect curCell = new Rect();
private int x;
private int y;
//保存输入的数据
private int nums[][] = new int[cols][cols];
public MyGridView2(Context context){
super(context);
}

public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode >= 8 && keyCode <= 16){
setNumValue(keyCode - 7);
}
invalidate();//重新再画一次
return super.onKeyDown(keyCode,event);
}

@Override
public boolean onTouchEvent(MotionEvent event){
//获取当前单元格的左上角的坐标
invalidate(curCell);
x = (int)((event.getX() - offset_x) / smallCellSize) * smallCellSize + offset_x;
y = (int)((event.getY() - offset_y) / smallCellSize) * smallCellSize + offset_y;
//修改矩形的尺寸
curCell.set(x,y,x+smallCellSize,y+smallCellSize);
//设置当前单元格为脏数据
invalidate(curCell);//要求重新绘制curCell
return super.onTouchEvent(event);

}

@Override
protected void onDraw(Canvas canvas){
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//获取画布的尺寸
int w,h;
w = getWidth();
h = getHeight();
//计算偏移量
//如果是h>w
if(h>w){
offset_x = 20;
bigCellSize = w - offset_x * 20;
smallCellSize = bigCellSize / cols;
offset_y = (h - bigCellSize) / 2;
}else{
offset_y = 20;
bigCellSize = bigCellSize / cols;
offset_x = (w - bigCellSize) / 2;
}

//画线
//定义线的样式
Paint linePaint = new Paint();
linePaint.setColor(Color.GREEN);
//画水平线
for(int i=0;i<=cols;i++){
canvas.drawLine(offset_x,offsex_y + i * smallCellSize, offset_x + bigCellSize, offset_y + i * smallCellSize, linePaint);
}
for(int i=0;i<=cols;i++){
canvas.drawLine(offset_x + i * smallCellSize, offset_y, offset_x + i * smallCellSize, offset_y + bigCellSize, linePaint);
}

//画当前的单元格
Paint curCellPaint = new Paint();
curCellPaint.setColor(Color.argb(160,255,0,0));
canvas.drawRect(curCell,curCellPaint);

//画数字
Paint textPaint = new Paint();
textPaint.setTextAlign(Paint.Align.CENTER);//放在中间
textPaint.setTextSize((int)(0.75 * smallCellSize));
textPaint.setColor(Color.GREEN);

for(int i=0;i<cols;i++){
for(int j=0;j<cols;j++){
float x = offset_x + j * smallCellSize + smallCellSize / 2;
float_y = offset_y + i * smallCellSize + smallCellSize / 2 - (fm.ascent + fm.descent) / 2;

canvas.drawText(getNumString(nums[i][j]), x,y,textPaint);
}
}

Log.d("ProjActivity","调用onDraw");
}

//把数字转化为字符串
private String getNumString(int num){
if(num == 0){
return "";
}else{
return num + "";
}
}

//设置数组的值
private void setNumValue(int value){
int ax,ay;
ax = (y - offset_y) / smallCellSize;
ay = (x - offset_x) / smallCellSize;
nums[ax][ay] = value;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐