package com.imooc_wuziqi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
public class WuziqiPanel extends View {
private int mPanelWidgh;
private int MAX_LINE=10;
private float mLineHeight;
private Paint mPaint=new Paint();
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();}
private void init()
{mPaint.setColor(0X88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
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);
mPanelWidgh=w;
mLineHeight=mPanelWidgh*1.0f/MAX_LINE;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
}
private void drawBoard(Canvas canvas) {
int w = mPanelWidgh;
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);
}
}
}
1 回答
汉江怪人
TA贡献50条经验 获得超11个赞
你的第63行
int y = (int) ((0.5 + i) * lineHeight);//把加号改成乘号
这样改应该就好了,i是第i条线,每条线之间间隔lineHeight那么大的距离刚开始从0.5开始估计是要和屏幕边界有间距吧,所以每次第i条线都要多lineHeight 所以要用乘的,i条线的y轴位置是i*lineHeight + 0.5*lineHeight,0.5*lineHeight是加上的边界
添加回答
举报
0/150
提交
取消