I am using android-svg and UniversalImageLoader for displaying svg pictures in a custom chess component. Though the chess component is a subclass of ImageView, I can't just use
ImageLoader.getInstance().displayImage()
as the picture does not fill the ImageView, but only a cell, and the cell dimension is computed at runtime.
I've tried to use the method loadImage
ImageLoader.getInstance().loadImage("assets://svg/red_x_cross.svg",
new ImageSize(sizePx, sizePx), loadedCompleteListener);
But it is as if I did not passed the ImageSize object : all images kept their original sizes.
Here is the configuration process :
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.EXACTLY)
.cacheOnDisk(true)
.cacheInMemory(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSize(175 * 1024)
.diskCacheSize(175 * 1024)
.imageDecoder(new SvgDecoder())
.defaultDisplayImageOptions(displayImageOptions)
.build();
ImageLoader.getInstance().init(config);
Here the SvgDecoder :
/**
* Utility class in order to configure Universal Image Loader library.
* Code from glide project sample
*/
public class SvgDecoder implements ImageDecoder {
@Override
public Bitmap decode(ImageDecodingInfo imageDecodingInfo) throws IOException {
InputStream is = imageDecodingInfo.getDownloader().getStream(
imageDecodingInfo.getImageUri(), imageDecodingInfo.getExtraForDownloader());
SVG svg;
try {
svg = SVG.getFromInputStream(is);
} catch (SVGParseException e) {
e.printStackTrace();
return null;
}
Picture picture = svg.renderToPicture();
PictureDrawable pictureDrawable = new PictureDrawable(picture);
// I think the problem is in this call parameters (getWidth/getHeight)
// but what must I write instead ?
Bitmap image = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),
pictureDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(image);
svg.renderToCanvas(imageCanvas);
return image;
}
}
All svg files I am using define a viewbox.
I think the problem comes from the SvgDecoder I copied-paste from the Glide library sample, but I don't know what should I write instead (where I put a comment).
My guess was quite good when I said that it is the responsibility of SvgDecoder class to set the image size. In fact I must
add the following line just after having built the bitmap (it will compute the final size for me)
decodedBitmap = considerExactScaleAndOrientatiton(decodedBitmap, decodingInfo, 0, false);