为了账号安全,请及时绑定邮箱和手机立即绑定

画不出棋盘线,手机模拟也不行

package com.example.wuziqi;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class WuziqiPanel extends View {
    private  int mPanelWidth;
    private float mLineHeight;
    private int MAX_LINE=10;
    private Paint mPaint = new Paint();
    public WuziqiPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(0x44ff0000);
        init();
    }

    private void init() {
        mPaint.setColor(0x8800000);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasure, int heightMeasure) {
        int widthSize = MeasureSpec.getSize(widthMeasure);
        int widthMode = MeasureSpec.getMode(widthMeasure);

        int heightSize = MeasureSpec.getSize(heightMeasure);
        int heightMode = MeasureSpec.getMode(heightMeasure);

        int width = Math.min(widthSize,heightSize);
        if(widthMode == MeasureSpec.UNSPECIFIED){
            width = heightSize;
        }else if (heightMode == MeasureSpec.UNSPECIFIED){
            width = widthSize;
        }
        setMeasuredDimension(width,width);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPanelWidth = w;
        mLineHeight = mPanelWidth * 1.0f / MAX_LINE;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
    }

    private void drawBoard(Canvas canvas) {
        int w = mPanelWidth;
        float lineHeight = mLineHeight;
        for (int i = 0;i<MAX_LINE;i++){
            int startX = (int)(lineHeight / 2);
            int endX  = (int)(w-lineHeight / 2);

            int y = (int) ((0.5+i)*lineHeight);
            canvas.drawLine(startX,y,endX,y,mPaint);
            canvas.drawLine(y,startX,y,endX,mPaint);
        }
    }
}


正在回答

2 回答

解决了吗

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android-五子连珠
  • 参与学习       39012    人
  • 解答问题       166    个

Android游戏开发-五子连珠,本教程通过UI与逻辑实现双人对战

进入课程

画不出棋盘线,手机模拟也不行

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信