根据发布的屏幕截图,顶部的绘图是使用画布创建的,底部的绘图是使用XML创建的。 XML绘图正是我想要的,但遗憾的是由于Android的观看次数太多而且很多。警告我使用它不是一个好主意:-(关于画布绘图是否有一些我缺少或不正确的事情导致绘图看起来不像XML一个或者我必须依赖价值观?我已经尝试过多种不同的方式,例如改变宽度和间距等值,我仍然没有运气。即使有人知道更好的方法来绘制这个,我&#39我希望了解它。所有相关的建议和帮助将不胜感激。
Java(画布)
public class Car extends View {
private final Paint mBlackPaint = new Paint();
private final Paint mRedPaint = new Paint();
private final TextPaint mTextPaint;
public Car(Context context, AttributeSet attrs) {
super(context, attrs);
int valueInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
int valueInSp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
mRedPaint.setColor(Color.parseColor("#CC3333"));
mBlackPaint.setAntiAlias(false);
mBlackPaint.setColor(Color.BLACK);
mBlackPaint.setStrokeWidth(valueInDp);
mBlackPaint.setStyle(Paint.Style.STROKE);
mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.BLACK);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(valueInSp);
mWindowPaint = new Paint();
mWindowPaint.setColor(Color.parseColor("#CC3333"));
mWindowPaint.setStrokeWidth(valueInDp);
}
private Paint mWindowPaint;
Rect rect = new Rect();
Rect rect2 = new Rect();
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getWidth() == 0)
return;
int w = canvas.getWidth();
int h = canvas.getHeight();
int rectWidth = ((w - 20) / 4) / 6;
int space = (((w - 20) / 4) / 13);
int oneSevenSpace = (((w - 20) / 4) / 6);
int oneSevenWidth = ((w - 20) / 4) / 6;
int topRectHeight = getPaddingTop();
int bottomRectHeight = getPaddingBottom();
//draw red rectangles
int mSideRectWidth = 10;
canvas.drawRect(0, 0, mSideRectWidth, getHeight(), mRedPaint); //draw left end rectangle
canvas.drawRect(getWidth() - mSideRectWidth, 0, getWidth(), getHeight(), mRedPaint); //draw right end rectangle
//draw carriages
setBackgroundColor(Color.parseColor("#808080"));
int boxWidth = (getWidth() - mSideRectWidth) / 4;
//draw text views
for (int i = 0; i < 4; i++) {
canvas.drawText(Integer.toString(i + 1), (i * boxWidth + 10) + (boxWidth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);
}
//draw box separators
for (int i = 1; i < 4; i++) {
canvas.drawLine(mSideRectWidth + boxWidth * i, 0, mSideRectWidth + boxWidth * i, getHeight(), mBlackPaint);
}
//draw windows
for (int index = 0; index < 4; index++) {
if (index == 0) {
for (int i = 0; i < 3; i++) {
mWindowPaint.setStyle(Paint.Style.STROKE);
int left = ((i * (oneSevenWidth + oneSevenSpace)) + 6) + (index * boxWidth) + 15 + (oneSevenSpace / 2);
int right = left + oneSevenWidth;
rect.set(left, 0, right, topRectHeight);
canvas.drawRect(rect, mWindowPaint);
rect2.set(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, mWindowPaint);
}
}
if (index == 1 || index == 2) {
for (int i = 0; i < 4; i++) {
mWindowPaint.setStyle(Paint.Style.STROKE);
int left = ((i * (rectWidth + space))) + (index * boxWidth) + 15 + (space / 2);
int right = left + rectWidth;
rect2.set(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, mWindowPaint);
if (index == 2 && i == 3) {
mWindowPaint.setStyle(Paint.Style.FILL_AND_STROKE);
}
rect.set(left, 0, right, topRectHeight);
canvas.drawRect(rect, mWindowPaint);
}
}
if (index == 3) {
for (int i = 0; i < 3; i++) {
mWindowPaint.setStyle(Paint.Style.STROKE);
int left = ((i * (oneSevenWidth + oneSevenSpace)) - 1) + ((index * boxWidth)) + (space * 2);
int right = left + oneSevenWidth;
rect.set(left, 0, right, topRectHeight);
canvas.drawRect(rect, mWindowPaint);
rect2.set(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, mWindowPaint);
}
}
}
}
}
XML
<View
android:layout_width="5dp"
android:layout_height="wrap_content"
android:background="#CC3333" />
<!--start of carriage 1-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#808080" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="1"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="4" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="4" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
</LinearLayout>
</RelativeLayout>
<!--end of carriage 1-->
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="#1D1D1D" />
<!--start of carriage 2-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#808080" >
<TextView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="2"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="6" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="6" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
</LinearLayout>
</RelativeLayout>
<!--end of carriage 2-->
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="#1D1D1D" />
<!--start of carriage 3-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#808080" >
<TextView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="3"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="6" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/fullrect_red" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="6" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="7" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
</LinearLayout>
</RelativeLayout>
<!--end of carriage 3-->
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="#1D1D1D" />
<!--start of carriage 4-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#808080" >
<TextView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="4"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@drawable/emptyrect_red" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="18" />
</LinearLayout>
</RelativeLayout>
<!--end of box 4-->
<View
android:layout_width="5dp"
android:layout_height="wrap_content"
android:background="#CC3333" />
结果
答案 0 :(得分:0)
您应该使用浮点数而不是整数来进行布局计算,然后在Android框架需要时将其强制转换为int。
int rectWidth = ((w - 20) / 4) / 6;
例如,rectWidth的值将被截断为最接近的整数。在以后的计算中,精度的损失更加复杂,导致了不同的行为。