Search in sources :

Example 1 with SVG

use of com.caverock.androidsvg.SVG in project glide by bumptech.

the class SvgDrawableTranscoder method transcode.

@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
Also used : SVG(com.caverock.androidsvg.SVG) Picture(android.graphics.Picture) PictureDrawable(android.graphics.drawable.PictureDrawable) SimpleResource(com.bumptech.glide.load.resource.SimpleResource)

Example 2 with SVG

use of com.caverock.androidsvg.SVG in project Carbon by ZieIony.

the class CheckableDrawable method renderSVGs.

private void renderSVGs() {
    Rect bounds = getBounds();
    if (bounds.width() <= 0 && bounds.height() <= 0)
        return;
    try {
        {
            SVG svg = SVG.getFromResource(context, checkedRes);
            checkedBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(checkedBitmap);
            svg.setDocumentWidth(checkedBitmap.getWidth());
            svg.setDocumentHeight(checkedBitmap.getHeight());
            svg.renderToCanvas(canvas);
            checkedShader = new BitmapShader(checkedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            matrix.postTranslate(bounds.left, bounds.top);
            checkedShader.setLocalMatrix(matrix);
        }
        {
            SVG svg2 = SVG.getFromResource(context, uncheckedRes);
            uncheckedBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(uncheckedBitmap);
            svg2.setDocumentWidth(uncheckedBitmap.getWidth());
            svg2.setDocumentHeight(uncheckedBitmap.getHeight());
            svg2.renderToCanvas(canvas);
        }
        {
            SVG svg3 = SVG.getFromResource(context, filledRes);
            filledBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(filledBitmap);
            svg3.setDocumentWidth(filledBitmap.getWidth());
            svg3.setDocumentHeight(filledBitmap.getHeight());
            svg3.renderToCanvas(canvas);
        }
        maskBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
        maskCanvas = new Canvas(maskBitmap);
        radius = (float) (Math.sqrt(2) * bounds.width() / 2);
    } catch (SVGParseException e) {
        Log.e(CheckableDrawable.class.getSimpleName(), "There was an error parsing SVG");
    } catch (NullPointerException e) {
    // TODO: what is this catch for?
    }
}
Also used : Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) SVG(com.caverock.androidsvg.SVG) SVGParseException(com.caverock.androidsvg.SVGParseException) Canvas(android.graphics.Canvas) BitmapShader(android.graphics.BitmapShader)

Example 3 with SVG

use of com.caverock.androidsvg.SVG in project android by nextcloud.

the class DisplayUtils method downloadSVGIcon.

private static void downloadSVGIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder, int width, int height) {
    GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder = Glide.with(context).using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class).from(Uri.class).as(SVG.class).transcode(new SvgDrawableTranscoder(), PictureDrawable.class).sourceEncoder(new StreamEncoder()).cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder(height, width))).decoder(new SvgDecoder(height, width)).placeholder(placeholder).error(placeholder).animate(android.R.anim.fade_in);
    Uri uri = Uri.parse(iconUrl);
    requestBuilder.diskCacheStrategy(DiskCacheStrategy.SOURCE).load(uri).into(imageView);
}
Also used : SVG(com.caverock.androidsvg.SVG) SvgDrawableTranscoder(com.owncloud.android.utils.svg.SvgDrawableTranscoder) InputStream(java.io.InputStream) PictureDrawable(android.graphics.drawable.PictureDrawable) SvgDecoder(com.owncloud.android.utils.svg.SvgDecoder) Uri(android.net.Uri) StreamEncoder(com.bumptech.glide.load.model.StreamEncoder)

Example 4 with SVG

use of com.caverock.androidsvg.SVG in project gh4a by slapperwan.

the class HttpImageGetter method renderSvgToBitmap.

private static Bitmap renderSvgToBitmap(Resources res, InputStream is, int maxWidth, int maxHeight) {
    // noinspection TryWithIdenticalCatches
    try {
        SVG svg = SVG.getFromInputStream(is);
        if (svg != null) {
            svg.setRenderDPI(DisplayMetrics.DENSITY_DEFAULT);
            Float density = res.getDisplayMetrics().density;
            int docWidth = (int) (svg.getDocumentWidth() * density);
            int docHeight = (int) (svg.getDocumentHeight() * density);
            if (docWidth < 0 || docHeight < 0) {
                float aspectRatio = svg.getDocumentAspectRatio();
                if (aspectRatio > 0) {
                    float heightForAspect = (float) maxWidth / aspectRatio;
                    float widthForAspect = (float) maxHeight * aspectRatio;
                    if (widthForAspect < heightForAspect) {
                        docWidth = Math.round(widthForAspect);
                        docHeight = maxHeight;
                    } else {
                        docWidth = maxWidth;
                        docHeight = Math.round(heightForAspect);
                    }
                } else {
                    docWidth = maxWidth;
                    docHeight = maxHeight;
                }
                // we didn't take density into account anymore when calculating docWidth
                // and docHeight, so don't scale with it and just let the renderer
                // figure out the scaling
                density = null;
            }
            while (docWidth >= maxWidth || docHeight >= maxHeight) {
                docWidth /= 2;
                docHeight /= 2;
                if (density != null) {
                    density /= 2;
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(docWidth, docHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            if (density != null) {
                canvas.scale(density, density);
            }
            svg.renderToCanvas(canvas);
            return bitmap;
        }
    } catch (SVGParseException e) {
    // fall through
    } catch (NullPointerException e) {
    // https://github.com/BigBadaboom/androidsvg/issues/81
    // remove me when there's a 1.2.3 release
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) SVG(com.caverock.androidsvg.SVG) SVGParseException(com.caverock.androidsvg.SVGParseException) Canvas(android.graphics.Canvas) Point(android.graphics.Point)

Example 5 with SVG

use of com.caverock.androidsvg.SVG in project android by nextcloud.

the class ActivityListAdapter method downloadIcon.

private void downloadIcon(Activity activity, ImageView itemViewType) {
    GenericRequestBuilder<Uri, InputStream, SVG, Bitmap> requestBuilder = Glide.with(context).using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class).from(Uri.class).as(SVG.class).transcode(new SvgBitmapTranscoder(128, 128), Bitmap.class).sourceEncoder(new StreamEncoder()).cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder())).decoder(new SvgDecoder()).placeholder(R.drawable.ic_activity).error(R.drawable.ic_activity).animate(android.R.anim.fade_in);
    Uri uri = Uri.parse(activity.getIcon());
    requestBuilder.diskCacheStrategy(DiskCacheStrategy.SOURCE).load(uri).into(itemViewType);
}
Also used : Bitmap(android.graphics.Bitmap) SVG(com.caverock.androidsvg.SVG) InputStream(java.io.InputStream) SvgBitmapTranscoder(com.owncloud.android.utils.svg.SvgBitmapTranscoder) SvgDecoder(com.owncloud.android.utils.svg.SvgDecoder) Uri(android.net.Uri) StreamEncoder(com.bumptech.glide.load.model.StreamEncoder)

Aggregations

SVG (com.caverock.androidsvg.SVG)16 PictureDrawable (android.graphics.drawable.PictureDrawable)8 SimpleResource (com.bumptech.glide.load.resource.SimpleResource)8 SVGParseException (com.caverock.androidsvg.SVGParseException)7 Bitmap (android.graphics.Bitmap)6 Canvas (android.graphics.Canvas)5 Uri (android.net.Uri)5 StreamEncoder (com.bumptech.glide.load.model.StreamEncoder)5 SvgDecoder (com.owncloud.android.utils.svg.SvgDecoder)5 InputStream (java.io.InputStream)5 Picture (android.graphics.Picture)4 SvgDrawableTranscoder (com.owncloud.android.utils.svg.SvgDrawableTranscoder)4 FileToStreamDecoder (com.bumptech.glide.load.resource.file.FileToStreamDecoder)2 BitmapShader (android.graphics.BitmapShader)1 Matrix (android.graphics.Matrix)1 Point (android.graphics.Point)1 Rect (android.graphics.Rect)1 Nullable (android.support.annotation.Nullable)1 Nullable (androidx.annotation.Nullable)1 CustomGlideUriLoader (com.owncloud.android.utils.glide.CustomGlideUriLoader)1