Android Canvas.drawPicture not working in devices with ice cream sandwich
Asked Answered
S

2

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.

Shih answered 30/4, 2012 at 13:37 Comment(1)
my target API is 7 <uses-sdk android:minSdkVersion="7"/>Shih
A
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);
    }
}
Anility answered 27/12, 2012 at 11:58 Comment(0)
T
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).

Theca answered 30/4, 2012 at 13:56 Comment(3)
But I only have the Picture object. I use link to parse svg files and it only gives Picture or PictureDrawable objects.Shih
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.Theca
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 PictureDrawableShih

© 2022 - 2024 — McMap. All rights reserved.