4

Okay, this is driving me nuts. I am using canvas to draw a gauge in my app. It also draws numbers on the hash marks and some horizontal text across the gauge. This all works fine in all versions of Android up to 4.2. In 4.2, it is only drawing one character (the middle character it seems) from the text it's supposed to write. For example, if I have text that reads 12345, it's only writing 3. If it's a two digit number, it only draws the first digit. I 'm seeing this behavior in the 4.2 emulator and on a Nexus 4 with 4.2.1. I read all about the hardware acceleration being turned on by default and that it causes problems with some Paint and Canvas features. I have inserted the code to turn off hardware acceleration for the drawText calls, but it doesn't help.

Here is my paint code:

titlePaint1 = new Paint();
titlePaint1.setColor(Color.WHITE);
titlePaint1.setAntiAlias(true);
titlePaint1.setTypeface(Typeface.DEFAULT_BOLD);
titlePaint1.setTextAlign(Paint.Align.CENTER);
titlePaint1.setTextSize(0.085f);

Here is the method called to draw the text:

@SuppressLint("NewApi")
private void drawTitle1(Canvas canvas) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        setLayerType(View.LAYER_TYPE_SOFTWARE, titlePaint1);

    canvas.drawText(title1, 0.5f, 0.72f, titlePaint1);
}

I pass it the canvas which is valid and the width and height are perfect. Everything else getting drawn on that canvas is displaying fine and there are three other calls to functions, like the one above, before it that draw the components of the gauge. It's just an issue with drawText. I know it has something to do with a change in 4.2 and I don't think this drawText issue has anything to do with hardware acceleration. I've only been able to find a few results on Google talking about Canvas issues with 4.2, but nothing has helped me to resolve my issue. Any ideas how I can resolve this and get the text to display properly?

ssuperz28
  • 1,834
  • 1
  • 14
  • 23
  • Your paints don't match up in your sample code. Could that be the issue? – ahodder Dec 13 '12 at 00:05
  • You are using the CENTER text alignment, which means that it's likely a clipping problem. Are you properly setting your clip rect? The fact that it happens both with software and hardware seems to indicate an issue in your code (the two pipeline use completely different implementations.) – Romain Guy Dec 13 '12 at 04:07
  • @AedonEtLIRA My bad on not having the matching titlePaint in the code samples. I was experimenting with titlePaint3, but titlePaint1 is the correct one and they both match in the code. – ssuperz28 Dec 13 '12 at 18:54
  • @RomainGuy I even tried it without the CENTER alignment and the results are the same. Actually on 4.2 with the CENTER alignment, the text is aligned left and without it, it looks centered (although, I think its aligning it to the right of the coordinate which is default). It must be a rectangle clipping problem if the coord is off to the left. What baffles me is it worked fine up to 4.2 and the rectangle that the text is being drawn within is the size of the gauge which is at minimum 300x300. I'm not very experienced with the drawing aspect of Android so I undoubtedly missed something. Thanks! – ssuperz28 Dec 13 '12 at 19:07
  • 1
    I've the same problem: http://stackoverflow.com/questions/13941270/android-4-2-on-nexus-7-canvas-drawtext-not-working-correctly – Seraphim's Dec 18 '12 at 21:16

2 Answers2

6

Thank you to "Seraphim's host" for the answer to this in their post. For any others having this issue with 4.2.1, the answer is to include setLinearText(true) on your paint for the text. This method is showing as deprecated, but it's the only solution for the text to display properly.

Here is the post with the answer: https://stackoverflow.com/a/13971632/1017328.

Community
  • 1
  • 1
ssuperz28
  • 1,834
  • 1
  • 14
  • 23
  • Happy to help! I'm still facing a kerning problem. See my other questione here: http://stackoverflow.com/questions/13974129/android-4-2-1-worng-character-kerning-spacing – Seraphim's Dec 28 '12 at 14:01
0

I strongly suspect that you have run into the bug I reported here: http://code.google.com/p/android/issues/detail?id=40965.

CvR
  • 151
  • 1
  • 7