在Android中下载并显示svg图像

时间:2018-03-12 09:00:21

标签: android svg download imageview picasso

我正在尝试下载并在我的ImageView中显示svg图像。我正在使用Picasso从网上显示我的其他图像。但是,现在我必须显示svg图像,我无法解码图像。

我就是这样做的其余部分: -

Picasso.with(context)
                .load(mProject.getAvatar())
                .placeholder(R.drawable.ic_description_blue_grey_600_36dp)
                .error(R.drawable.ic_description_blue_grey_600_36dp)
                .centerCrop()
                .into(holder.thumbnail);

如何从网上显示SVG图像?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

使用此库 AndroidSvgLoader

答案 1 :(得分:0)

在svg文件的代码下面用作图像视图..

private class HttpImageRequestTask extends AsyncTask<Void, Void, Drawable> {
    @Override
    protected Drawable doInBackground(Void... params) {
        try {


            final URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/e/e8/Svg_example3.svg");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            SVG svg = SVGParser. getSVGFromInputStream(inputStream);
            Drawable drawable = svg.createPictureDrawable();
            return drawable;
        } catch (Exception e) {
            Log.e("MainActivity", e.getMessage(), e);
        }

        return null;
    }

    @Override
    protected void onPostExecute(Drawable drawable) {
        // Update the view
        updateImageView(drawable);
    }
}
private void updateImageView(Drawable drawable){
    if(drawable != null){

        // Try using your library and adding this layer type before switching your SVG parsing
        mImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        Picasso.with(this)
                .placeholder(drawable) //this is optional the image to display while the url image is downloading
                .error(Your Drawable Resource)         //this is also optional if some error has occurred in downloading the image this image would be displayed
                .into(imageView);

    }
}