代码之家  ›  专栏  ›  技术社区  ›  user3034944

在Android中下载并显示svg图像

  •  1
  • user3034944  · 技术社区  · 8 年前

    我正在尝试在ImageView中下载并显示svg图像。我正在使用毕加索显示我在网络上的其他图像。但是,现在我必须显示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);
    

    如何从web显示SVG图像?非常感谢您的帮助。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Vivek Pratap Singh    8 年前

    使用此库 AndroidSvgLoader

        2
  •  -1
  •   Mobile Team ADR-Flutter    8 年前

    以下代码用于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);
    
        }
    }
    
    推荐文章