Java 自定义视图(画布与XML)

Java 自定义视图(画布与XML),java,android,xml,android-layout,android-canvas,Java,Android,Xml,Android Layout,Android Canvas,根据发布的屏幕截图,顶部的图形是使用画布创建的,底部的图形是使用XML创建的。XML绘图正是我想要的,但不幸的是,由于Android的“视图太多”警告,使用它不是一个好主意:-(关于画布绘图,是否有我遗漏或做得不好的地方导致绘图与XML绘图不完全相同,或者我必须依赖于值?我尝试了许多不同的方法,例如更改宽度和间距值等,但我仍然没有运气。即使有人知道更好的绘制方法他的,我想知道。所有相关的建议和帮助将不胜感激 Java(画布) 公共级汽车扩展视图{ 私人最终油漆mBlackPaint=新油漆();

根据发布的屏幕截图,顶部的图形是使用画布创建的,底部的图形是使用XML创建的。XML绘图正是我想要的,但不幸的是,由于Android的“视图太多”警告,使用它不是一个好主意:-(关于画布绘图,是否有我遗漏或做得不好的地方导致绘图与XML绘图不完全相同,或者我必须依赖于值?我尝试了许多不同的方法,例如更改宽度和间距值等,但我仍然没有运气。即使有人知道更好的绘制方法他的,我想知道。所有相关的建议和帮助将不胜感激

Java(画布)

公共级汽车扩展视图{
私人最终油漆mBlackPaint=新油漆();
私人最终油漆mRedPaint=新油漆();
私人最终文本绘制mTextPaint;
公共汽车(上下文、属性集属性){
超级(上下文,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(假);
mBlackPaint.setColor(颜色:黑色);
mBlackPaint.设置行程宽度(值INDP);
mBlackPaint.setStyle(油漆.样式.笔划);
mTextPaint=新的TextPaint(TextPaint.ANTI_别名_标志);
mTextPaint.setColor(颜色为黑色);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(值INSP);
mWindowPaint=新油漆();
mWindowPaint.setColor(Color.parseColor(#CC3333”);
mWindowPaint.设置行程宽度(值INDP);
}
私人油漆;
Rect Rect=新的Rect();
Rect rect2=新的Rect();
@覆盖受保护的void onDraw(画布){
super.onDraw(帆布);
如果(getWidth()==0)
回来
int w=canvas.getWidth();
inth=canvas.getHeight();
宽度=((w-20)/4)/6;
int空间=((w-20)/4)/13);
int-oneSevenSpace=((w-20)/4)/6);
int-oneSevenWidth=((w-20)/4)/6;
int-topRectHeight=getPaddingTop();
int bottomRectHeight=getPaddingBottom();
//画红色的长方形
int mSideRectWidth=10;
drawRect(0,0,mSideRectWidth,getHeight(),mRedPaint);//绘制左端矩形
drawRect(getWidth()-mSideRectWidth,0,getWidth(),getHeight(),mRedPaint);//绘制右端矩形
//拉车
setBackgroundColor(Color.parseColor(#808080”);
int-boxWidth=(getWidth()-mSideRectWidth)/4;
//绘制文本视图
对于(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);
}
//牵引箱分离器
对于(int i=1;i<4;i++){
画布.绘图线(mSideRectWidth+boxWidth*i,0,mSideRectWidth+boxWidth*i,getHeight(),mBlackPaint);
}
//画窗
对于(int-index=0;index<4;index++){
如果(索引==0){
对于(int i=0;i<3;i++){
mWindowPaint.setStyle(油漆.样式.笔划);
int left=((i*(oneSevenWidth+oneSevenSpace))+6+(index*boxWidth)+15+(oneSevenSpace/2);
int right=左+七分之一宽;
矩形集(左、0、右、topRectHeight);
画布.drawRect(rect,mWindowPaint);
设置(左,h-底部矩形高度,右,h);
画布.drawRect(rect2,mWindowPaint);
}
}
如果(索引=1 | |索引=2){
对于(int i=0;i<4;i++){
mWindowPaint.setStyle(油漆.样式.笔划);
int left=((i*(矩形宽度+空格))+(索引*boxWidth)+15+(空格/2);
int right=左+矩形宽度;
设置(左,h-底部矩形高度,右,h);
画布.drawRect(rect2,mWindowPaint);
如果(索引=2&&i==3){
mWindowPaint.setStyle(油漆、样式、填充和笔划);
}
矩形集(左、0、右、topRectHeight);
画布.drawRect(rect,mWindowPaint);
}
}
如果(索引==3){
对于(int i=0;i<3;i++){
mWindowPaint.setStyle(油漆.样式.笔划);
int left=(i*(oneSevenWidth+oneSevenSpace))-1+((index*boxWidth))+(空格*2);
int right=左+七分之一宽;
矩形集(左、0、右、topRectHeight);
画布.drawRect(rect,mWindowPaint);
设置(左,h-底部矩形高度,右,h);
画布.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" />


结果

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);
                }
            }
        }
    }
}
int rectWidth = ((w - 20) / 4) / 6;