Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour of `drawTextOnPath()` with hardware accelration

In hardware accelerated custom View added in ScrollView or ListView both of the following code snippets produces same result: (please ignore best practises for a sec)

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); 
    // centering stuff
    float centerX = getWidth() / 2f;
    float centerY = getHeight() / 2f;
    float size = 80;
    float halfSize = size / 2f;
    float left = centerX - halfSize;
    float top = centerY - halfSize;
    
    RectF oval = new RectF(left, top, left + size, top + size);
    
    Path path = new Path();
    path.addArc(oval, 160, 359);
    
    Paint paint = new Paint();
    paint.setTextSize(30);
    paint.setStyle(Style.STROKE);
    
    canvas.drawTextOnPath("Hello world", path, 0, 0, paint); //<--- line A
    canvas.drawCircle(centerX, centerY, 10, paint);          //<--- line B
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); 
    // centering stuff
    float centerX = getWidth() / 2f;
    float centerY = getHeight() / 2f;
    float size = 80;
    float halfSize = size / 2f;
    float left = centerX - halfSize;
    float top = centerY - halfSize;
    
    RectF oval = new RectF(left, top, left + size, top + size);
    
    Path path = new Path();
    path.addArc(oval, 160, 359);
    
    Paint paint = new Paint();
    paint.setTextSize(30);
    paint.setStyle(Style.STROKE);
    
    canvas.drawCircle(centerX, centerY, 10, paint);          //<--- line B
    canvas.drawTextOnPath("Hello world", path, 0, 0, paint); //<--- line A
}

Same Result:

enter image description here


But with later code snippet, as soon as you scroll the ScrollView (I have invisible dummy View below so I can scroll) and helloworld touches ActionBar, something very intersting happens and you see something that intelligent humankind used to see in old Windows OS .

enter image description here


enter image description here

I know drawTextOnPath() is not supported in hardware accelration mode, but then why it works if you call it first?

like image 490
M-WaJeEh Avatar asked Nov 02 '22 18:11

M-WaJeEh


1 Answers

drawTextOnPath() is supported by hardware acceleration after Android 4.1 This is mentioned officially here: https://code.google.com/p/android/issues/detail?id=37925 but the next comment seems to indicate your problem in a way so maybe a bug.

Of course for pre 4.1 just dont make it use HW accel - Set a software layer type on your View by calling View.setLayerType(View.LAYER_TYPE_SOFTWARE, null) and try to get a tradeoff on perf vs errors

like image 65
Vrashabh Irde Avatar answered Nov 15 '22 11:11

Vrashabh Irde