下载并显示 Android 中的 svg 图像
Download and display svg image in Android
我正在尝试下载 svg 图像并将其显示在我的 ImageView 中。我正在使用 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 图像?感谢任何帮助。
使用这个库
AndroidSvgLoader
使用下面的代码将 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);
}
}
我正在尝试下载 svg 图像并将其显示在我的 ImageView 中。我正在使用 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 图像?感谢任何帮助。
使用这个库 AndroidSvgLoader
使用下面的代码将 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);
}
}