【问题标题】:Download and display svg image in Android在 Android 中下载并显示 svg 图像
【发布时间】:2018-08-20 05:53:05
【问题描述】:

我正在尝试下载并在我的 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);

如何显示来自网络的 SVG 图像?任何帮助表示赞赏。

【问题讨论】:

标签: android svg download imageview picasso


【解决方案1】:

使用这个库 AndroidSvgLoader

【讨论】:

  • 我试过这个。在将其添加到 gradle 时,它​​给了我一个错误,说 Error:(52, 13) Failed to resolve: com.github.ar-android:AndroidSvgLoader:1.0.1
  • 在你的项目 build.gradle 中添加这一行——在他们的 github 页面上清楚地提到了如何使用这个 lib allprojects { repositories { google() jcenter() maven { url 'jitpack.io' } }
  • 我也添加了
  • 你能发布你的 build.gradle 文件吗?我刚刚构建它的工作正常。
  • 谢谢。将其添加到 gradle 时出现了一些问题。
【解决方案2】:

用于 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);

    }
}

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多