nan0133
nan0133

Reputation: 71

Import SVG images

I just want to see some SVG images, when i click on the app. I tried it. The steps to import into android studio was correct. But when i run the apk, i can see only a blank screen. The SVG image did'nt show up. So what can i do? How can i correct it? The code i used is,

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create a new ImageView

        ImageView imageView = new ImageView(this);
        // Set the background color to white
        imageView.setBackgroundColor(Color.BLACK);
        // Parse the SVG file from the resource
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
        // Get a drawable from the parsed SVG and set it as the drawable for the ImageView
        imageView.setImageDrawable(svg.createPictureDrawable());
        // Set the ImageView as the content view for the Activity
        setContentView(imageView);
    }

Upvotes: 0

Views: 2194

Answers (1)

Paul LeBeau
Paul LeBeau

Reputation: 101800

Your problem is likely caused by one of these two things:

  1. Hardware acceleration is turned on by default on recent Android devices and this causes issues as SVG rendering can require some features of Canvas that are not supported by the hardware accelearated renderer on some versions of Android. Each version of Android gets better, but you will probably have to explicitly tell it to use the software renderer.

See: Having issue on Real Device using vector image in android. SVG-android

  1. When you say "android-svg" I assume you mean "svg-android"? svg-android is not maintained and doesn't support a lot of SVG features. This may be the reason your SVG is not rendering. Try a newer fork, or another library like AndroidSVG.

Upvotes: 1

Related Questions