android:load svg file from web and show it on image view

android:load svg file from web and show it on image view(android:load svg file from web and show it on image view)

本文介绍了android:load svg file from web and show it on image view的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网上加载一个 svg 文件并在 ImageView 中显示这个文件.对于非矢量图像,我使用 Picasso 库.

I want to load a svg file from the web and show this file in an ImageView. For non vector images I use the Picasso library.

是否也可以将此库用于 svg 文件?
有没有办法从网络加载 svg 文件并将其显示在 ImageView 中?
我使用 svg-android 库来显示 svg 文件,但我不知道如何要从网络上获取 svg 图像,该库的所有示例都使用本地文件.

Is it possible to use this library for svg files as well?
Is there any way to load svg files from the web and show it in an ImageView?
I use the svg-android library to show svg files but i don't know how to get svg images from the web all the examples for this library use local files.

推荐答案

请参考在真实设备上使用 android 中的矢量图像时出现问题.SVG-android

在用户帖子中,他提出了类似的问题并建议他使用:

In the users post he asks a similar question and suggest he uses:

在布局文件中为 ImageView 创建一个成员变量;

Create a member variable for the ImageView in your layout file;

private ImageView mImageView;

// intialize in onCreate(Bundle savedInstanceState)
mImageView = (ImageView) findViewById(R.id.image_view);

下载图片

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);
    }
}

然后将drawable应用到Imageview

Then Apply the drawable to the Imageview

@SuppressLint("NewApi")
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);
        mImageView.setImageDrawable(drawable);
    }
}

SVGParser 可在 https://github.com/pents90/svg-android

SVGParser is available at https://github.com/pents90/svg-android

这篇关于android:load svg file from web and show it on image view的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:android:load svg file from web and show it on image view

基础教程推荐