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
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01