绘制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;
}
}
创建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;
}
}
相关文章推荐
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 04.游戏界面 主角飞机的添加
- 安卓第二课:教材中游戏启动界面实现之准备知识
- Android-使用XML布局文件实现游戏的开始界面
- 简化 RecyclerView 的多种 type 的 adapter,Fragment 可以动态添加到 RecyclerView 上,实现复杂的界面分多个模块开发
- unity_NGUI系统学习(十四)_游戏界面滑动切换的效果实现(无代码实现)
- android 程式开发:(十)绘制用户界面 —— 10.1通过纯编码实现UI
- javascript实现后购物界面添加一类
- ACtivity实现欢迎界面并添加动画切换效果
- IOS:手写签名的实现(实现了手势绘制字体,添加文字水印,图片剪切、图片压缩)
- cocos2d-x 实现按Home和待机键后,再进入游戏显示暂停界面
- 在游戏中实现跳转到webView界面(Android实现)
- 用Ogre MyGUI实现一套完整的游戏界面及实现方法介绍
- 利用SDL+CEGUI框架实现VTP的游戏界面风格
- Windows Phone 7范例游戏Platformer实战7——简单动画的绘制实现原理
- Unity中如何用代码实现横版游戏路径的绘制
- msn中实现 "添加一个活动或游戏邀请"
- 如何在ogre游戏界面添加自己的信息,比如显示某变量的值
- [Android] 触屏setOnTouchListener实现图片缩放、移动、绘制和添加水印
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- 用Ogre MyGUI实现一套完整的游戏界面及实现方法介绍