Android开发——手把手写APP(四)——数独游戏编写(一)——绘制九宫格

数独游戏开发(一)绘制九宫格


B站数独开发视频教程
上述视频是本人该系列博客所学视频。

九宫格,分为九条横线,九条竖线,其中,有三条横线和竖线是加深的。

绘制九宫格的九条浅色线

为了让九宫格的线看上去跟刻的一样,特意画两条线,中间隔一像素即可。

public class ShuduView extends View {

    //单元格的宽度和高度
    private  float width;
    private float height;
    public ShuduView(Context context) {
        super(context);
    }

    @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) {
        super.onDraw(canvas);
        //生成用于绘制背景色的画笔
        Paint backgroundPaint = new Paint();
        //设置画笔的颜色
        backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));
        //绘制背景色
        canvas.drawRect(0,0,getWidth(),getHeight(),backgroundPaint);

        //深色画笔
        Paint drakPaint = new Paint();
        drakPaint.setColor(getResources().getColor(R.color.shudu_dark));

        Paint hilitePaint = new Paint();
        hilitePaint.setColor(getResources().getColor(R.color.shudu_hilite));

        //浅色画笔
        Paint lightPaint = new Paint();
        lightPaint.setColor(getResources().getColor(R.color.shudu_light));

        //画出九宫格的9根浅色线
        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,lightPaint);
            canvas.drawLine(i * width,0,i * width,getHeight(),lightPaint);
            canvas.drawLine(i * width + 1,0,i * width + 1,getHeight(),lightPaint);
        }
}

在这里插入图片描述

绘制九宫格的三条深色线

        //画出九宫格的3跟深色线
       for(int i = 0;i < 9;i++){
            if(i%3!=0){
                continue;
            }
            canvas.drawLine(0,i * height,getWidth(),i * height,drakPaint);
            canvas.drawLine(0,i * height + 1,getWidth(),i * height + 1,drakPaint);
            canvas.drawLine(i * width,0,i * width,getHeight(),drakPaint);
            canvas.drawLine(i * width + 1,0,i * width + 1,getHeight(),drakPaint);
        }

在这里插入图片描述

发布了230 篇原创文章 · 获赞 250 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/weixin_42247720/article/details/103499425