java– 如何在画布中的现有矩形顶部添加矩形

java– 如何在画布中的现有矩形顶部添加矩形,第1张

概述我试图在现有画布添加一些红色矩形,与特定的盒子一样,与预期的结果图像完全一样,但它们根本不显示,因为我的代码显示了部署应用程序时当前不需要的结果.我的代码是在顶行创建4个矩形,在底行创建4个矩形,但我只想在盒子2-6的顶部添加它,但我知道需要为盒子顶部的红色矩形添加额外

我试图在现有画布中添加一些红色矩形,与特定的盒子一样,与预期的结果图像完全一样,但它们根本不显示,因为我的代码显示了部署应用程序时当前不需要的结果.我的代码是在顶行创建4个矩形,在底行创建4个矩形,但我只想在盒子2-6的顶部添加它,但我知道需要为盒子顶部的红色矩形添加额外的代码1& 7.有谁知道我做错了什么以及如何解决这个问题?所有帮助将不胜感激.

public class RectangleTextVIEw extends VIEw {    private final Paint mBlackPaint = new Paint();    private final Paint mRedPaint = new Paint();    private final TextPaint mTextPaint;    public RectangleTextVIEw(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;    @OverrIDe protected voID onDraw(Canvas canvas) {        super.onDraw(canvas);        if (getWIDth() == 0)            return;        //initialise red rectangles        int w = canvas.getWIDth();        int h = canvas.getHeight();        int rectWIDth = w / 5;        int space = w / 15;        int topRectHeight = getpaddingtop();        int bottomrectHeight = getpaddingBottom();        //draw end 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 grey Boxes        setBackgroundcolor(color.parsecolor("#808080"));        int BoxWIDth = (getWIDth() - mSIDeRectWIDth) / 7;        //draw text vIEws        for (int i = 0; i < 7; i++) {            canvas.drawText(Integer.toString(i + 1), (i * BoxWIDth + 10) + (BoxWIDth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);        }        //draw black lines        for (int i = 1; i < 7; i++) {            canvas.drawline(mSIDeRectWIDth + BoxWIDth * i, 0, mSIDeRectWIDth + BoxWIDth * i, getHeight(), mBlackPaint);        }        //draw red windows        for (int i = 0; i < 4; i++) {            mWindowPaint.setStyle(Paint.Style.stroke);//add this            int left = i * (rectWIDth + space);            int right = left + rectWIDth;            if (i == 1){                mWindowPaint.setStyle(Paint.Style.FILL); // change to this            }            Rect rect = new Rect(left, 0, right, topRectHeight);            canvas.drawRect(rect, mWindowPaint);            Rect rect2 = new Rect(left, h - bottomrectHeight, right, h);            canvas.drawRect(rect2, mWindowPaint);        }    }}

预期结果


当前不受欢迎的结果

activity_main.xml中

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <com.apptacularapps.car.RectangleTextVIEw        androID:layout_wIDth="100dp"        androID:layout_height="45dp"        androID:paddingtop="10dp"        androID:paddingBottom="10dp"        androID:background="#808080"        androID:gravity="center"/></relativeLayout>

MainActivity.java

import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);    }}

解决方法:

问题是你在屏幕尺寸上只创建了4个矩形,而不是数字单元格大小.
这是代码:

public class RectangleTextVIEw extends VIEw {    private final Paint mBlackPaint = new Paint();    private final Paint mRedPaint = new Paint();    private final TextPaint mTextPaint;    public RectangleTextVIEw(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;        //initialise red rectangles        int w = canvas.getWIDth();        int h = canvas.getHeight();        int rectWIDth = ((w - 20) / 7) / 5;        int space = ((w - 20) / 7) / 15;        int topRectHeight = getpaddingtop();        int bottomrectHeight = getpaddingBottom();        //draw end 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 grey Boxes        setBackgroundcolor(color.parsecolor("#808080"));        int BoxWIDth = (getWIDth() - mSIDeRectWIDth) / 7;        //draw text vIEws        for (int i = 0; i < 7; i++) {            canvas.drawText(Integer.toString(i + 1), (i * BoxWIDth + 10) + (BoxWIDth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);        }        //draw black lines        for (int i = 1; i < 7; i++) {            canvas.drawline(mSIDeRectWIDth + BoxWIDth * i, 0, mSIDeRectWIDth + BoxWIDth * i, getHeight(), mBlackPaint);        }        //draw red windows        for (int index = 0; index < 7; index++) {            if (index == 0 || index == 6) {                for (int i = 0; i < 3; i++) {                    mWindowPaint.setStyle(Paint.Style.stroke);//add this                    int left = (i * (rectWIDth + space)) + (index * BoxWIDth) + 13 + rectWIDth/2 + space/2;                    int right = left + rectWIDth;                    rect.set(left, 0, right, topRectHeight);                    canvas.drawRect(rect, mWindowPaint);                    if (index == 0 && i == 1) {                        mWindowPaint.setStyle(Paint.Style.FILL); // change to this                    }                    rect2.set(left, h - bottomrectHeight, right, h);                    canvas.drawRect(rect2, mWindowPaint);                }            } else {                for (int i = 0; i < 4; i++) {                    mWindowPaint.setStyle(Paint.Style.stroke);//add this                    int left = (i * (rectWIDth + space)) + (index * BoxWIDth) + 13;                    int right = left + rectWIDth;                    rect.set(left, 0, right, topRectHeight);                    canvas.drawRect(rect, mWindowPaint);                    rect2.set(left, h - bottomrectHeight, right, h);                    canvas.drawRect(rect2, mWindowPaint);                }            }        }    }}

这是完整的代码完全适合我.如果您有任何疑问或怀疑随意发布:)

这就是我看到它们的方式:

总结

以上是内存溢出为你收集整理的java – 如何在画布中的现有矩形顶部添加矩形全部内容,希望文章能够帮你解决java – 如何在画布中的现有矩形顶部添加矩形所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102339.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存