问题描述:

I am calculating function graphs and put them into android.graphics.Path objects and then use android.graphics.Canvas.drawPath() to draw it.

This works really well unless absolute y-values very high (either positive or negative) and the app is running on certain devices. Then, it might not draw the path at all. The threshold of when this happens seems to be random and device specific.

Does anyone know what might be the problem here?

[edit: This occurs only if target API-Level is 14 or higher]

Edit:

For example, take a look at this code within the View.onDraw(Canvas canvas) method:

 Paint paint = new Paint();

Path p1 = new Path();

paint.setColor(Color.BLUE);

for(float x = 0; x < getWidth(); x++)

p1.lineTo(x, x);

canvas.drawPath(p1, paint);

Path p2 = new Path();

paint.setColor(Color.GREEN);

for(float x = 0; x < getWidth(); x++)

p2.lineTo(x, x*x);

canvas.drawPath(p2, paint);

The first path should be a line coming from the the upper left corner and going to the bottom right; the second path should do the same but have the shape of a parabola (and, because of that, you can only see part of it).

The first path draws, the second does not, apparently because y-values get too high. It would be great to know what "too high" means though ;)

网友答案:

I found the solution.

The problem was, that from API-Level 14, all drawing is done hardware accelerated which does not support all the functionality.

As explained in this article, it is simple to turn hardware acceleration off with:

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

You will find more information at this blog post.

相关阅读:
Top