BigBadaboom / androidsvg

SVG rendering library for Android
http://bigbadaboom.github.io/androidsvg/
Apache License 2.0
1.21k stars 231 forks source link

svg problem #213

Closed Rahmahry closed 2 years ago

Rahmahry commented 3 years ago

. Hello. I'm sorry but i have little bit of a problem.

              final URL url = new URL(url);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                SVG svg = SVG.getFromInputStream(inputStream);
                bitmap = Bitmap.createBitmap((int) Math.ceil(svg.getDocumentWidth()),
                        (int) Math.ceil(svg.getDocumentHeight()),
                        Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                svg.renderToCanvas(canvas);

It returns low image quality. How to fix it? A svg image has low resolution (It's very little) but it's svg. It must be good. If i use phone with big resolution then image has low quality but it's svg. It must be good. Any methods to parse svg to get good quality for all resolutions? Sorry for my bad Ensglish

BigBadaboom commented 3 years ago

Hi Rahmahry

You possibly don't want to be using getDocumentWidth() and getDocumentHeight(). What those values are will depend on what the width and height attributes are in you SVG.

What are you doing with the Bitmap once you have rendered the SVG to it? It would help to know that in order to give you the best advice.

It would also help to see the SVG you are using.

Paul

Rahmahry commented 3 years ago

Thanks for answering. Now i use another method: svg.renderToPicture() and an image quality is really better but still isn't perfect. Also i have another problem. I tested my code on real device (android 5) and it doesn't load image to my ImageView. If i use an emulator (android 9) it loads image good.

My full code:

public abstract class Loader_2 {
    private Activity activity;
    String url_pic = "";
    Bitmap bitmap;
    Drawable drawable;

    public Loader_2(Activity activity, String url_pic) {
        this.activity = activity;
        this.url_pic = url_pic;
    }

    private void startBackground() {
        new Thread(new Runnable() {
            public void run() {
             //   bitmap = doInBackground();
                try {
                    final URL url = new URL(url_pic);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    InputStream inputStream = urlConnection.getInputStream();
                    SVG svg = SVG.getFromInputStream(inputStream);
                   //
                    bitmap = Bitmap.createBitmap((int) Math.ceil(svg.getDocumentWidth()),
                            (int) Math.ceil(svg.getDocumentHeight()),
                            Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    svg.renderToCanvas(canvas);
                    //
                    drawable = new PictureDrawable(svg.renderToPicture());
                } catch (SVGParseException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        onPostExecute();
                    }
                });
            }
        }).start();
    }

    public void execute(){
        startBackground();
    }
    public abstract void onPostExecute();

}

MainActivity:

        final Loader_2 loader_2 = new Loader_2(MainActivity.this, url2) {
            @Override
            public void onPostExecute() {
                if(bitmap != null  && drawable != null) {
                    //   imageView.setImageBitmap(bitmap);
                    imageView.setImageDrawable(drawable);
                    imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                            txtx.setText("rX: "+imageView.getWidth());
                            txty.setText("rY: "+imageView.getHeight());
                         //   writePolygons(full_x,full_y, old_x, old_y, frame);

                            frame.removeAllViews();
                            onWindowFocusChanged(true);
                            frame.removeAllViews();
                        }
                    });
                } else {
                    Toast.makeText(getApplicationContext(),"Error of loading" , Toast.LENGTH_SHORT).show();
                }
            }
        };
        loader_2.execute();

My svg: svg.zip

BigBadaboom commented 3 years ago

Hi

Check this FAQ entry, and see if it solves your problem.

[My SVG doesn't render if I try to do Canvas.drawPicture()](https://bigbadaboom.github.io/androidsvg/faq.html#My_SVG_doesn't_render_if_I_try_to_do_Canvas.drawPicture()_o)


Your SVG has hardwired width and hight values: width="52.258327mm" height="60.303696mm". That corresponds to about 198 x 228px. That's pretty small. Try overriding those values:

SVG svg = SVG.getFromInputStream(inputStream);
svg.setDocumentWidth("100%");
svg.setDocumentHeight("100%");
drawable = new PictureDrawable(svg.renderToPicture())   // or renderToCanvas() either whould work

More information here

BigBadaboom commented 2 years ago

Closing this old issue.