繪製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;
}
}