private Resource<Bitmap> getTransformedFrame(Bitmap currentFrame, Transformation<Bitmap> transformation, GifDrawable drawable) { // TODO: what if current frame is null? Resource<Bitmap> bitmapResource = factory.buildFrameResource(currentFrame, bitmapPool); Resource<Bitmap> transformedResource = transformation.transform( context, bitmapResource, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); if (!bitmapResource.equals(transformedResource)) { bitmapResource.recycle(); } return transformedResource; }
@Override public void draw(@NonNull Canvas canvas) { if (isRecycled) { return; } if (applyGravity) { Gravity.apply(GRAVITY, getIntrinsicWidth(), getIntrinsicHeight(), getBounds(), getDestRect()); applyGravity = false; } Bitmap currentFrame = state.frameLoader.getCurrentFrame(); canvas.drawBitmap(currentFrame, null, getDestRect(), getPaint()); }
@Test public void testWritesTransformedBitmaps() { final Bitmap frame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(frame); when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); int expectedWidth = 123; int expectedHeight = 456; when(gifDrawable.getIntrinsicWidth()).thenReturn(expectedWidth); when(gifDrawable.getIntrinsicHeight()).thenReturn(expectedHeight); Bitmap transformedFrame = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565); when(transformedResource.get()).thenReturn(transformedFrame); when(frameTransformation.transform( anyContext(), eq(frameResource), eq(expectedWidth), eq(expectedHeight))) .thenReturn(transformedResource); when(gifDrawable.getFrameTransformation()).thenReturn(frameTransformation); encoder.encode(resource, file, options); verify(gifEncoder).addFrame(eq(transformedFrame)); }
@Test @SuppressWarnings("unchecked") public void testSetsTransformationAsFrameTransformation() { Resource<GifDrawable> resource = mockResource(); GifDrawable gifDrawable = mock(GifDrawable.class); Transformation<Bitmap> unitTransformation = UnitTransformation.get(); when(gifDrawable.getFrameTransformation()).thenReturn(unitTransformation); when(gifDrawable.getIntrinsicWidth()).thenReturn(500); when(gifDrawable.getIntrinsicHeight()).thenReturn(500); when(resource.get()).thenReturn(gifDrawable); Bitmap firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(gifDrawable.getFirstFrame()).thenReturn(firstFrame); final int width = 123; final int height = 456; Bitmap expectedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Resource<Bitmap> expectedResource = mockResource(); when(expectedResource.get()).thenReturn(expectedBitmap); when(wrapped.transform(any(Context.class), Util.<Bitmap>anyResource(), anyInt(), anyInt())) .thenReturn(expectedResource); transformation.transform(context, resource, width, height); verify(gifDrawable).setFrameTransformation(isA(Transformation.class), eq(expectedBitmap)); }
@Test public void testReturnsFrameWidthAndHeightForIntrinsicDimensions() { assertEquals(frameWidth, drawable.getIntrinsicWidth()); assertEquals(frameHeight, drawable.getIntrinsicHeight()); }
private Resource<Bitmap> getTransformedFrame(Bitmap currentFrame, Transformation<Bitmap> transformation, GifDrawable drawable) { // TODO: what if current frame is null? Resource<Bitmap> bitmapResource = factory.buildFrameResource(currentFrame, bitmapPool); Resource<Bitmap> transformedResource = transformation.transform(bitmapResource, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); if (!bitmapResource.equals(transformedResource)) { bitmapResource.recycle(); } return transformedResource; }
@Override public void draw(Canvas canvas) { if (isRecycled) { return; } if (applyGravity) { Gravity.apply(GifState.GRAVITY, getIntrinsicWidth(), getIntrinsicHeight(), getBounds(), destRect); applyGravity = false; } Bitmap currentFrame = frameLoader.getCurrentFrame(); Bitmap toDraw = currentFrame != null ? currentFrame : state.firstFrame; canvas.drawBitmap(toDraw, null, destRect, paint); }
@Override public boolean onResourceReady(GifDrawable resource, String model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { listener.onResourceReady(resource.getIntrinsicWidth(), resource.getIntrinsicHeight()); return false; } }).into(imageView);
@Override public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) { int w = MeasureUtil.getScreenSize(mContext).x; int hh=resource.getIntrinsicHeight(); int ww=resource.getIntrinsicWidth() ; int high = hh * (w - 50)/ww; Rect rect = new Rect(20, 20,w-30,high); resource.setBounds(rect); urlDrawable.setBounds(rect); urlDrawable.setDrawable(resource); gifDrawables.add(resource); resource.setCallback(mTextView); resource.start(); resource.setLoopCount(GlideDrawable.LOOP_FOREVER); mTextView.setText(mTextView.getText()); mTextView.invalidate(); } }