How do I setMaskfilter without crash on Android O?

2019-07-15 06:56发布

问题:

I use a RecyclerView in my grid. Each item has textview and Imageview. I set mask filter for textview in my Adapter as below:

float[] direction = new float[]{0.0f, -1.0f, 0.5f};
MaskFilter maskFilter = new EmbossMaskFilter(direction, 0.8f, 15f, 1f);
holder.textview.getPaint().setMaskFilter(maskFilter);

It is the reason of crash (Fatal signal 11 (SIGSEGV), code 1, fault addr 0xfff8022c in tid 32638 (ReferenceQueueD)) for the firs time run.

回答1:

This is known bug. Problem in method canvas.drawText(); But I found a way to avoid crash, by using canvas.drawPath(); instead.

Path path = new Path();
paint.getTextPath( text, 0, text.length(), x, y, path );
canvas.drawPath( path, paint );

If you want to use it in TextView you have to extend this class and create new onDraw method. Enjoy!