Search in sources :

Example 1 with SimpleResource

use of com.bumptech.glide.load.resource.SimpleResource 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 SimpleResource

use of com.bumptech.glide.load.resource.SimpleResource in project glide by bumptech.

the class DrawableTransformationTest method transform_withColorDrawable_andUnitBitmapTransformation_recycles.

@Test
public void transform_withColorDrawable_andUnitBitmapTransformation_recycles() {
    bitmapPool = mock(BitmapPool.class);
    Glide.tearDown();
    Glide.init(context, new GlideBuilder().setBitmapPool(bitmapPool));
    when(bitmapTransformation.transform(any(Context.class), anyBitmapResource(), anyInt(), anyInt())).thenAnswer(new ReturnGivenResource());
    ColorDrawable colorDrawable = new ColorDrawable(Color.RED);
    final Resource<Drawable> input = new SimpleResource<Drawable>(colorDrawable);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Bitmap bitmap = (Bitmap) invocationOnMock.getArguments()[0];
            assertThat(bitmap.getWidth()).isEqualTo(100);
            assertThat(bitmap.getHeight()).isEqualTo(200);
            return null;
        }
    }).when(bitmapPool).put(any(Bitmap.class));
    when(bitmapPool.get(anyInt(), anyInt(), any(Bitmap.Config.class))).thenAnswer(new Answer<Bitmap>() {

        @Override
        public Bitmap answer(InvocationOnMock invocationOnMock) throws Throwable {
            int width = (Integer) invocationOnMock.getArguments()[0];
            int height = (Integer) invocationOnMock.getArguments()[1];
            Bitmap.Config config = (Bitmap.Config) invocationOnMock.getArguments()[2];
            return Bitmap.createBitmap(width, height, config);
        }
    });
    transformation.transform(context, input, /*outWidth=*/
    100, /*outHeight=*/
    200);
    verify(bitmapPool).put(isA(Bitmap.class));
}
Also used : Context(android.content.Context) Config(org.robolectric.annotation.Config) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapPool(com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GlideBuilder(com.bumptech.glide.GlideBuilder) SimpleResource(com.bumptech.glide.load.resource.SimpleResource) Test(org.junit.Test)

Example 3 with SimpleResource

use of com.bumptech.glide.load.resource.SimpleResource in project android by nextcloud.

the class SvgDecoder method decode.

public Resource<SVG> decode(InputStream source, int w, int h) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        if (width > 0) {
            svg.setDocumentWidth(width);
        }
        if (height > 0) {
            svg.setDocumentHeight(height);
        }
        svg.setDocumentPreserveAspectRatio(PreserveAspectRatio.LETTERBOX);
        return new SimpleResource<>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
Also used : SVG(com.caverock.androidsvg.SVG) SVGParseException(com.caverock.androidsvg.SVGParseException) SimpleResource(com.bumptech.glide.load.resource.SimpleResource) IOException(java.io.IOException)

Example 4 with SimpleResource

use of com.bumptech.glide.load.resource.SimpleResource in project android by nextcloud.

the class SvgBitmapTranscoder method transcode.

@Override
public Resource<Bitmap> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    try {
        svg.setDocumentHeight("100%");
        svg.setDocumentWidth("100%");
    } catch (SVGParseException e) {
        Log_OC.e(this, "Could not set document size. Output might have wrong size");
    }
    // Create a canvas to draw onto
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    // Render our document onto our canvas
    svg.renderToCanvas(canvas);
    return new SimpleResource<>(bitmap);
}
Also used : Bitmap(android.graphics.Bitmap) SVG(com.caverock.androidsvg.SVG) SVGParseException(com.caverock.androidsvg.SVGParseException) Canvas(android.graphics.Canvas) SimpleResource(com.bumptech.glide.load.resource.SimpleResource)

Example 5 with SimpleResource

use of com.bumptech.glide.load.resource.SimpleResource in project PocketHub by pockethub.

the class SvgDrawableTranscoder method transcode.

@Nullable
@Override
public Resource<Drawable> transcode(@NonNull Resource<SVG> toTranscode, @NonNull Options options) {
    SVG svg = toTranscode.get();
    float ratio = svg.getDocumentAspectRatio();
    int width = svg.getDocumentWidth() > 0 ? (int) svg.getDocumentWidth() : 1024;
    int height = (int) (svg.getDocumentHeight() > 0 ? svg.getDocumentHeight() : (width / ratio));
    Picture picture = svg.renderToPicture(width, height);
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<>(drawable);
}
Also used : SVG(com.caverock.androidsvg.SVG) Picture(android.graphics.Picture) PictureDrawable(android.graphics.drawable.PictureDrawable) SimpleResource(com.bumptech.glide.load.resource.SimpleResource) Nullable(androidx.annotation.Nullable)

Aggregations

SimpleResource (com.bumptech.glide.load.resource.SimpleResource)9 SVG (com.caverock.androidsvg.SVG)8 Bitmap (android.graphics.Bitmap)4 Picture (android.graphics.Picture)4 PictureDrawable (android.graphics.drawable.PictureDrawable)4 SVGParseException (com.caverock.androidsvg.SVGParseException)4 Canvas (android.graphics.Canvas)2 Context (android.content.Context)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Nullable (android.support.annotation.Nullable)1 Nullable (androidx.annotation.Nullable)1 GlideBuilder (com.bumptech.glide.GlideBuilder)1 BitmapPool (com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Config (org.robolectric.annotation.Config)1