自定义View画出可变数量的分割线
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (widthMeasureSpec < heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
} else {
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
width = getWidth();
height = getHeight();
paint = new Paint();
paint.setColor(Color.BLACK);
//给一个可变的数量
num = 5;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//先画出横排的线
for (int i = 0; i < num; i++) {
float startX = width / num * (i + 1);
float startY = 0;
float stopX = width / num * (i + 1);
float stopY = height;
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
//再画出竖排的线
for (int i = 0; i < num; i++) {
float startX2 = 0;
float startY2 = height / num * (i + 1);
float stopX2 = width;
float stopY2 = height / num * (i + 1);
canvas.drawLine(startX2, startY2, stopX2, stopY2, paint);
}
}
这样的话就可以根据可变的数量来画线进行分割
转载请注明原文地址: https://ju.6miu.com/read-1201390.html