1、绘制九宫格
2、在九宫格中放置数字
MyView.java文件:
package com.example.shudu03;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class MyView extends View {
public MyView(Context context) {
super(context);
}
private float width;
private float height;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.width=w/9f;
this.height=h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
//画背景
Paint paintBackgroud=new Paint();
paintBackgroud.setColor(getResources().getColor(R.color.backgroundColor));
canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackgroud);
//画第一条颜色重的线的画笔
Paint paintOne=new Paint();
paintOne.setColor(getResources().getColor(R.color.oneLine));
//画第二条颜色轻的线的画笔
Paint paintTwo=new Paint();
paintTwo.setColor(getResources().getColor(R.color.twoLine));
//画横竖三条颜色重的线
Paint paintThree=new Paint();
paintThree.setColor(getResources().getColor(R.color.threeLine));
for(int i=0;i<9;i++){
canvas.drawLine(0, i*height, getWidth(), i*height, paintOne);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, paintTwo);
canvas.drawLine(i*width, 0, i*width, getHeight(), paintOne);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), paintTwo);
}
for(int i=0;i<9;i++){
if(i%3 != 0){
continue;
}
canvas.drawLine(0, i*height, getWidth(), i*height, paintThree);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, paintTwo);
canvas.drawLine(i*width, 0, i*width, getHeight(),paintThree);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), paintTwo);
}
//画数字的画笔
Paint paintData=new Paint();
paintData.setColor(getResources().getColor(R.color.textColor));
paintData.setStyle(Paint.Style.STROKE);
paintData.setTextSize((float) (height*0.75));
paintData.setTextAlign(Paint.Align.CENTER);
float x=width/2;
canvas.drawText("1", 3*width+x, 61, paintData);
super.onDraw(canvas);
}
}
color.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="backgroundColor">#ffe2e5e7</color>
<color name="textColor">#ff252525</color>
<color name="oneLine">#ffeceff0</color>
<color name="twoLine">#fffbfbfb</color>
<color name="threeLine">#ff858585</color>
</resources>
MainActivity.java文件:
package com.example.shudu03;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}