0

I'm trying to have one image be a background for another, using the svg-android library. When I view the image on my Samsung Galaxy Nexus (4.3, Sprint), the background image doesn't display. When I view it on the emulator, it's there correctly. I've even removed the foreground, and this stays the same. Here's the key parts of the XML layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.kd7uiy.hamfinder.SvgView
        android:id="@+id/svgImage"
        android:src="@raw/paper"
        android:layout_alignLeft="@+id/scroll"
        android:layout_alignRight="@id/scroll"
        android:layout_alignTop="@id/scroll"
        android:layout_above="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ScrollView
            android:id="@+id/scroll"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/buttons"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="@android:color/transparent"
            android:layout_below="@id/adView">
        <TableLayout
            android:id="@+id/tableView"
                android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="1"
            android:stretchColumns="1"
        />
    </ScrollView>
</RelativeLayout>

And the custom class. Note I tried a tip from svg-android without luck:

public class SvgView extends View {

    Picture picture;

    long startTime;
    float scaleFactor;

    public SvgView(Context context,AttributeSet attrs) {
        super(context,attrs);
        setImage(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src", 0));
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        {
            disableHardwareAcceleration();
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void disableHardwareAcceleration()
    {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public SvgView(Context context) {
        super(context);

    }

    public void setImage(int img_resource)
    {
        // Parse the SVG file from the resource
        SVG svg = SVGParser.getSVGFromResource(getResources(),img_resource);
        picture = svg.getPicture();
    }

    public void onDraw(Canvas canvas) {
        if (picture!=null)
        {
            scaleFactor=Math.min((float)getHeight()/picture.getHeight(),(float)getWidth()/picture.getWidth());
            canvas.scale((float)scaleFactor,(float)scaleFactor);
            canvas.drawPicture(picture);
        }

    }
}

Lastly, here's the image from my Galaxy Nexus, and the emulator. Note the difference in the background.

enter image description here

enter image description here

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • I am having problem in this question http://stackoverflow.com/questions/20400697/canvas-is-not-showing-image-drawable-andorid . can you please help me out in solving this problem – User42590 Dec 05 '13 at 13:27

1 Answers1

0

It turns out the provided tip was in the right direction, but I had a small typo. The hardware acceleration seems to be the culprit. As I don't have anything that complex, disabling it is just fine. I almost had the right solution, just had the wrong comparitor. Changing the code as follows fixes it:

public SvgView(Context context,AttributeSet attrs) {
    super(context,attrs);
    setImage(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src", 0));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        disableHardwareAcceleration();
    }
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • As far as I can tell you don't need to disable hardware acceleration on all versions starting from Honeycomb. The problem is that some drawing functions were not implemented on certain OS versions, but on Kitkat it works just fine. I am not sure the exact API version where it won't cause any trouble, it might even depending on the rendered SVG. But for me everything above JELLY_BEAN_MR2 seems to be working fine. – racs Feb 22 '15 at 05:09