3

I want to draw a Picture on a Canvas by

mCanvas.drawpicture(mPicture, mRect)

Using target API 7 <uses-sdk android:minSdkVersion="7"/>, it works perfectly in devices with API<14, but in devices with Ice Cream Sandwich, it doesn't work. Apparently this is because canvas.drawPicture is not supported with Hardware Acceleration: Unsupported Drawing Operations I have tried to fix this by disabling the Hardware Acceleration in the Manifest:

<application android:hardwareAccelerated="false" ...>

but still does't work.

Behzad Momahed Heravi
  • 1,383
  • 1
  • 12
  • 17

2 Answers2

6

I had the same problem and solved by programmatically turning off hardware acceleration only on the view that will draw the Picture

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

However setLayerType is only supported since API 11. So use this method instead:

public static void setHardwareAccelerated(View view, boolean enabled){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        if(enabled)
            view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        else view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
}
ffleandro
  • 4,039
  • 4
  • 33
  • 48
5

Try replacing drawPicture with drawBitmap. The syntax is almost the same, you just need to pass a source rectangle (just make it the size of the image) and a paint (which if you're not editing the image can be null).

David Scott
  • 1,666
  • 12
  • 22
  • But I only have the Picture object. I use [link](http://code.google.com/p/svg-android/) to parse svg files and it only gives Picture or PictureDrawable objects. – Behzad Momahed Heravi Apr 30 '12 at 14:40
  • You should be able to get a bitmap from a `PictureDrawable` with `getBitmap()`. I'm not sure what impact this will have on an SVG though. – David Scott Apr 30 '12 at 14:48
  • 2
    ok. I used PictureDrawable and draw on canvas: `mPictureDrawable.setBounds(mRect); mPictureDrawable.draw(mCanvas);` It works now, thanks. But still was hoping to use Picture instead of PictureDrawable – Behzad Momahed Heravi Apr 30 '12 at 14:55