XML : Shapes not appearing despite declaring them / Avoid object allocations

I've just created a canvas drawing to try and achieve the expected result but for some reason a warning is returned + the rectangles don't appear at all hence I end up with undesired result. Does anyone know what on earth is going on with the small red rectangles not appearing and how to get rid of this warning?

Avoid object allocations during draw/layout operations (preallocate and reuse instead)

expected result

enter image description here

undesired result

enter image description here

  public class RectangleTextView extends View {      private final Paint mBlackPaint = new Paint();      private final Paint mRedPaint = new Paint();      private final TextPaint mTextPaint;      private final Paint mWindowPaint = new Paint();        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.setColor(Color.parseColor("#CC3333"));          mWindowPaint.setStrokeWidth(valueInDp);      }          @Override      protected void onDraw(Canvas canvas) {          super.onDraw(canvas);          if (getWidth() == 0)              return;            //initialise red rectangles          int h = canvas.getHeight();              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;              int redRectWidth = boxWidth / 5;          int redRectSpace = redRectWidth / 3;            //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);                int baseStartX = i * boxWidth;                //draw red windows              for (int j = 0; j < 4; j++) {                  mWindowPaint.setStyle(Paint.Style.STROKE);//add this                  int left = mSideRectWidth + baseStartX + (j * (redRectWidth + redRectSpace));                  int right = left + redRectWidth;                  if (j == 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);              }          }            //draw black lines          for (int i = 1; i < 7; i++) {                int startX = mSideRectWidth + boxWidth * i;              int startY = 0;              int stopX = mSideRectWidth + boxWidth * i;              int stopY = getHeight();              canvas.drawLine(startX, startY, stopX, stopY, mBlackPaint);          }      }  }    

enter image description here

No comments:

Post a Comment