4

I have a custom view that draws several different things on screen. Each one has its own paint object. Everything is drawing fine EXCEPT the text. It works just fine in Gingerbread, but ICS+ has no text.

Here is my on draw method:

protected void onDraw(Canvas canvas)
{
   canvas.save(Canvas.MATRIX_SAVE_FLAG);
   canvas.scale(getWidth(), getHeight());

   drawGrid(canvas);
   drawHeader(canvas);
   drawSelected(canvas);
   drawDays(canvas);
   drawToday(canvas);

   canvas.restore();
}

Grid, Selected, and Today work fine. Header and Days are the text drawing and they don't work.

Here is the drawHeader method:

private void drawHeader(Canvas canvas)
{
   canvas.drawText("Sun", DAYS[0], .05f, paintDaysOfTheWeek);
   canvas.drawText("Mon", DAYS[1], .05f, paintDaysOfTheWeek);
   canvas.drawText("Tues", DAYS[2], .05f, paintDaysOfTheWeek);
   canvas.drawText("Wed", DAYS[3], .05f, paintDaysOfTheWeek);
   canvas.drawText("Thurs", DAYS[4], .05f, paintDaysOfTheWeek);
   canvas.drawText("Fri", DAYS[5], .05f, paintDaysOfTheWeek);
   canvas.drawText("Sat", DAYS[6], .05f, paintDaysOfTheWeek);

   canvas.drawLine(.01f, .0f, .99f, .0f, paintMediumBlack);
   canvas.drawLine(.01f, .07f, .99f, .07f, paintMediumBlack);
}

Any ideas?

toadzky
  • 261
  • 3
  • 12

2 Answers2

3

I fixed the issue by adding

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    setLayerType(LAYER_TYPE_SOFTWARE, paint)

to my custom view. Not sure why I needed it, and if anyone could explain it, that would be great.

toadzky
  • 261
  • 3
  • 12
  • 2
    This is an incredibly frustrating issue that I've been dealing with for the past 2 days. Setting the layer type to software worked for me as well, but I don't understand why! In onDraw - drawCircle, drawRect work, but drawPath and drawText did not! Anyone explain? – autonomy Jan 28 '14 at 20:30
1

When hardware acceleration is enabled, it sometimes makes optmizations by removing drawing calls that it thinks they aren't needed.

For example if you have a View beneath another View it can decide not to render it assuming it is hidden so there's no need to render it.

Oren Bengigi
  • 994
  • 9
  • 17