2

On ICS device, I tried the following code to draw two rectangles.

Path p1 = new Path();
p1.moveTo(0, 0);
p1.lineTo(0, 100);
p1.lineTo(100, 100);
p1.lineTo(100, 0);
p1.close;
Path p2 = new Path();
Matrix scaling = new Matrix();
scaling.preScale(2, 2);
p1.transform(scaling, p2);
canvas.drawPath(p1);
canvas.drawPath(p2);

Running the above code on ICS device with hardware acceleration enabled (as it is by default), p1 is drawn where as p2 is not.

In general, what happened to me is, as long as a Path is not hand-wired (i.e. by calling lineTo(), quadTo(), etc.), but obtained by copying or transforming (i.e. by calling the copy constructor, transform(matrix, dest), translate(x, y, dest), etc.), it is not drawn.

I found a "widely known" issue that is similar but not exactly the same as my problem: https://groups.google.com/forum/#!msg/android-developers/eTxV4KPy1G4/tAe2zUPCjMcJ

Therefore, can anyone tell me what is the issue I am running into? In my case, I have to resort to path transformation otherwise code complexity will be greatly increase. Thanks!

user690421
  • 422
  • 1
  • 5
  • 15
  • I'm not sure about your issue, but I can tell you that canvas has a method for drawing rectangles canvas.drawRect(...). Also only turn on the acceleration if(!isInEditMode() && isGardwareAccelerationPossible()) – Bojan Kseneman Apr 20 '15 at 17:46
  • @BojanKseneman Thanks! But I'd like to make no assumption whether the path is rectangle or not as I'd like my code to work with arbitrary path. – user690421 Apr 20 '15 at 21:29

1 Answers1

0

try setting android:layerType="software" in xml on the view to see if that fixes it. some methods aren't available with hardware acceleration on all apis.

the list is here:

http://developer.android.com/guide/topics/graphics/hardware-accel.html

note that if changing the layer type fixes it, you should create a separate layout for the newer APIs for optimal performance

dabluck
  • 1,641
  • 2
  • 13
  • 20