代码:@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
Log.e("TAG", "OnMeasure" + ":" + width + ":" + height + "||" + widthMeasureSpec + ":" + heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// height = getHeight();
float w = 100; //width为屏幕宽 取1/2为边长
float h = (float) Math.sqrt(3) * w; //高 根号3倍边长
path.moveTo((1 / 2) * w, 0);
path.lineTo(0, h / 2);
path.lineTo((1 / 2) * w, h);
path.lineTo((3 / 2) * w, h);
path.lineTo(2 * w, h / 2);
path.lineTo((3 / 2) * w, 0);
path.lineTo((1 / 2) * w, 0);
path.close();
canvas.drawPath(path, paint);
Log.e("TAG", "执行到这里2" + "::" + w + "||" + h);
}
之前我是使用屏幕的宽来作为六边形的边长的,后来显示错误我就写死边长为100,然并卵,下面是效果图和XML代码。
XML代码:
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
宽高我之前也试过写死固定的值还是不行。
效果图:
到底是怎么回事啊?大神求救!!!!