com.facebook.imagepipeline.image.CloseableAnimatedImage Java Examples

The following examples show how to use com.facebook.imagepipeline.image.CloseableAnimatedImage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoxingFrescoLoader.java    From boxing with Apache License 2.0 6 votes vote down vote up
private Drawable createDrawableFromFetchedResult(Context context, CloseableImage image) {
    if (image instanceof CloseableStaticBitmap) {
        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image;
        BitmapDrawable bitmapDrawable = createBitmapDrawable(context, closeableStaticBitmap.getUnderlyingBitmap());
        return (closeableStaticBitmap.getRotationAngle() != 0 && closeableStaticBitmap.getRotationAngle() != -1 ? new OrientedDrawable(bitmapDrawable, closeableStaticBitmap.getRotationAngle()) : bitmapDrawable);
    } else if (image instanceof CloseableAnimatedImage) {
        AnimatedDrawableFactory animatedDrawableFactory = Fresco.getImagePipelineFactory().getAnimatedFactory().getAnimatedDrawableFactory(context);
        if (animatedDrawableFactory != null) {
            AnimatedDrawable animatedDrawable = (AnimatedDrawable) animatedDrawableFactory.create(image);
            if (animatedDrawable != null) {
                return animatedDrawable;
            }
        }
    }
    throw new UnsupportedOperationException("Unrecognized image class: " + image);
}
 
Example #2
Source File: AnimatedImageFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private CloseableImage getCloseableImage(ImageDecodeOptions options, AnimatedImage image) {
  int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0;
  CloseableReference<Bitmap> previewBitmap = null;
  if (options.decodePreviewFrame) {
    previewBitmap = createPreviewBitmap(image, frameForPreview);
  }
  try {
    AnimatedImageResult animatedImageResult = AnimatedImageResult.newBuilder(image)
        .setPreviewBitmap(previewBitmap)
        .setFrameForPreview(frameForPreview)
        .build();
    return new CloseableAnimatedImage(animatedImageResult);
  } finally {
    CloseableReference.closeSafely(previewBitmap);
  }
}
 
Example #3
Source File: AnimatedSingleUsePostprocessorProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testNonStaticBitmapIsPassedOn() {
  SingleUsePostprocessorConsumer postprocessorConsumer = produceResults();
  CloseableAnimatedImage sourceCloseableAnimatedImage = mock(CloseableAnimatedImage.class);
  CloseableReference<CloseableImage> sourceCloseableImageRef =
      CloseableReference.<CloseableImage>of(sourceCloseableAnimatedImage);
  postprocessorConsumer.onNewResult(sourceCloseableImageRef, Consumer.IS_LAST);
  sourceCloseableImageRef.close();
  mTestExecutorService.runUntilIdle();

  mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.IS_LAST));
  mInOrder.verifyNoMoreInteractions();

  assertEquals(1, mResults.size());
  CloseableReference<CloseableImage> res0 = mResults.get(0);
  assertTrue(CloseableReference.isValid(res0));
  assertSame(sourceCloseableAnimatedImage, res0.get());
  res0.close();

  verify(sourceCloseableAnimatedImage).close();
}
 
Example #4
Source File: AnimatedRepeatedPostprocessorProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testNonStaticBitmapIsPassedOn() {
  RepeatedPostprocessorConsumer postprocessorConsumer = produceResults();
  RepeatedPostprocessorRunner repeatedPostprocessorRunner = getRunner();

  CloseableAnimatedImage sourceCloseableAnimatedImage = mock(CloseableAnimatedImage.class);
  CloseableReference<CloseableImage> sourceCloseableImageRef =
      CloseableReference.<CloseableImage>of(sourceCloseableAnimatedImage);
  postprocessorConsumer.onNewResult(sourceCloseableImageRef, Consumer.IS_LAST);
  sourceCloseableImageRef.close();
  mTestExecutorService.runUntilIdle();

  mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.NO_FLAGS));
  mInOrder.verifyNoMoreInteractions();

  assertEquals(1, mResults.size());
  CloseableReference<CloseableImage> res0 = mResults.get(0);
  assertTrue(CloseableReference.isValid(res0));
  assertSame(sourceCloseableAnimatedImage, res0.get());
  res0.close();

  performCancelAndVerifyOnCancellation();
  verify(sourceCloseableAnimatedImage).close();
}
 
Example #5
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 6 votes vote down vote up
private void testCreateDefaults(WebPImage mockWebPImage, PooledByteBuffer byteBuffer) {
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeWebP(
              encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockWebPImage, imageResult.getImage());
  assertNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verifyZeroInteractions(mMockAnimatedDrawableBackendProvider);
  verifyZeroInteractions(mMockBitmapFactory);
}
 
Example #6
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 6 votes vote down vote up
private void testCreateDefaults(GifImage mockGifImage, PooledByteBuffer byteBuffer) {
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeGif(
              encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockGifImage, imageResult.getImage());
  assertNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verifyZeroInteractions(mMockAnimatedDrawableBackendProvider);
  verifyZeroInteractions(mMockBitmapFactory);
}
 
Example #7
Source File: PipelineDraweeController.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Drawable createDrawable(CloseableReference<CloseableImage> image) {
  Preconditions.checkState(CloseableReference.isValid(image));
  CloseableImage closeableImage = image.get();
  if (closeableImage instanceof CloseableStaticBitmap) {
    CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
    return new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
  } else if (closeableImage instanceof CloseableAnimatedImage) {
    return mAnimatedDrawableFactory.create(
        ((CloseableAnimatedImage) closeableImage).getImageResult());
  } else {
    throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
  }
}
 
Example #8
Source File: AnimatedImageFactoryImpl.java    From fresco with MIT License 5 votes vote down vote up
private CloseableImage getCloseableImage(
    ImageDecodeOptions options, AnimatedImage image, Bitmap.Config bitmapConfig) {
  List<CloseableReference<Bitmap>> decodedFrames = null;
  CloseableReference<Bitmap> previewBitmap = null;
  try {
    final int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0;
    if (options.forceStaticImage) {
      return new CloseableStaticBitmap(
          createPreviewBitmap(image, bitmapConfig, frameForPreview),
          ImmutableQualityInfo.FULL_QUALITY,
          0);
    }

    if (options.decodeAllFrames) {
      decodedFrames = decodeAllFrames(image, bitmapConfig);
      previewBitmap = CloseableReference.cloneOrNull(decodedFrames.get(frameForPreview));
    }

    if (options.decodePreviewFrame && previewBitmap == null) {
      previewBitmap = createPreviewBitmap(image, bitmapConfig, frameForPreview);
    }
    AnimatedImageResult animatedImageResult =
        AnimatedImageResult.newBuilder(image)
            .setPreviewBitmap(previewBitmap)
            .setFrameForPreview(frameForPreview)
            .setDecodedFrames(decodedFrames)
            .setBitmapTransformation(options.bitmapTransformation)
            .build();
    return new CloseableAnimatedImage(animatedImageResult);
  } finally {
    CloseableReference.closeSafely(previewBitmap);
    CloseableReference.closeSafely(decodedFrames);
  }
}
 
Example #9
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 5 votes vote down vote up
private void testCreateWithPreviewBitmap(
    WebPImage mockWebPImage, PooledByteBuffer byteBuffer, Bitmap mockBitmap) throws Exception {
  // For decoding preview frame, expect some calls.
  final AnimatedDrawableBackend mockAnimatedDrawableBackend =
      createAnimatedDrawableBackendMock(1);

  when(mMockAnimatedDrawableBackendProvider.get(
          any(AnimatedImageResult.class), isNull(Rect.class)))
      .thenReturn(mockAnimatedDrawableBackend);
  when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG))
      .thenReturn(CloseableReference.of(mockBitmap, FAKE_BITMAP_RESOURCE_RELEASER));
  AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
  PowerMockito.whenNew(AnimatedImageCompositor.class)
      .withAnyArguments()
      .thenReturn(mockCompositor);

  ImageDecodeOptions imageDecodeOptions =
      ImageDecodeOptions.newBuilder().setDecodePreviewFrame(true).build();
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);
  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeWebP(
              encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockWebPImage, imageResult.getImage());
  assertNotNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verify(mMockAnimatedDrawableBackendProvider)
      .get(any(AnimatedImageResult.class), isNull(Rect.class));
  verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
  verify(mMockBitmapFactory).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
  verifyNoMoreInteractions(mMockBitmapFactory);
  verify(mockCompositor).renderFrame(0, mockBitmap);
}
 
Example #10
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 5 votes vote down vote up
private void testCreateWithPreviewBitmap(
    GifImage mockGifImage, Bitmap mockBitmap, PooledByteBuffer byteBuffer) throws Exception {
  // For decoding preview frame, expect some calls.
  final AnimatedDrawableBackend mockAnimatedDrawableBackend =
      createAnimatedDrawableBackendMock(1);
  when(mMockAnimatedDrawableBackendProvider.get(
          any(AnimatedImageResult.class), isNull(Rect.class)))
      .thenReturn(mockAnimatedDrawableBackend);
  when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG))
      .thenReturn(CloseableReference.of(mockBitmap, FAKE_BITMAP_RESOURCE_RELEASER));
  AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
  PowerMockito.whenNew(AnimatedImageCompositor.class)
      .withAnyArguments()
      .thenReturn(mockCompositor);

  ImageDecodeOptions imageDecodeOptions =
      ImageDecodeOptions.newBuilder().setDecodePreviewFrame(true).build();
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);
  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeGif(
              encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockGifImage, imageResult.getImage());
  assertNotNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verify(mMockAnimatedDrawableBackendProvider)
      .get(any(AnimatedImageResult.class), isNull(Rect.class));
  verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
  verify(mMockBitmapFactory).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
  verifyNoMoreInteractions(mMockBitmapFactory);
  verify(mockCompositor).renderFrame(0, mockBitmap);
}
 
Example #11
Source File: ExperimentalBitmapAnimationDrawableFactory.java    From fresco with MIT License 4 votes vote down vote up
@Override
public boolean supportsImageType(CloseableImage image) {
  return image instanceof CloseableAnimatedImage;
}
 
Example #12
Source File: ExperimentalBitmapAnimationDrawableFactory.java    From fresco with MIT License 4 votes vote down vote up
@Override
public AnimatedDrawable2 createDrawable(CloseableImage image) {
  return new AnimatedDrawable2(
      createAnimationBackend(((CloseableAnimatedImage) image).getImageResult()));
}
 
Example #13
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 4 votes vote down vote up
private void testCreateWithDecodeAlFrames(
    WebPImage mockWebPImage, PooledByteBuffer byteBuffer, Bitmap mockBitmap1, Bitmap mockBitmap2)
    throws Exception {
  // For decoding preview frame, expect some calls.
  final AnimatedDrawableBackend mockAnimatedDrawableBackend =
      createAnimatedDrawableBackendMock(2);
  when(mMockAnimatedDrawableBackendProvider.get(
          any(AnimatedImageResult.class), isNull(Rect.class)))
      .thenReturn(mockAnimatedDrawableBackend);

  when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG))
      .thenReturn(CloseableReference.of(mockBitmap1, FAKE_BITMAP_RESOURCE_RELEASER))
      .thenReturn(CloseableReference.of(mockBitmap2, FAKE_BITMAP_RESOURCE_RELEASER));
  AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
  PowerMockito.whenNew(AnimatedImageCompositor.class)
      .withAnyArguments()
      .thenReturn(mockCompositor);

  ImageDecodeOptions imageDecodeOptions =
      ImageDecodeOptions.newBuilder()
          .setDecodePreviewFrame(true)
          .setDecodeAllFrames(true)
          .build();

  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeWebP(
              encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockWebPImage, imageResult.getImage());
  assertNotNull(imageResult.getDecodedFrame(0));
  assertNotNull(imageResult.getDecodedFrame(1));
  assertNotNull(imageResult.getPreviewBitmap());

  // Should not have interacted with these.
  verify(mMockAnimatedDrawableBackendProvider)
      .get(any(AnimatedImageResult.class), isNull(Rect.class));
  verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
  verify(mMockBitmapFactory, times(2)).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
  verifyNoMoreInteractions(mMockBitmapFactory);
  verify(mockCompositor).renderFrame(0, mockBitmap1);
  verify(mockCompositor).renderFrame(1, mockBitmap2);
}
 
Example #14
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 4 votes vote down vote up
private void testCreateWithDecodeAlFrames(
    GifImage mockGifImage, Bitmap mockBitmap1, Bitmap mockBitmap2, PooledByteBuffer byteBuffer)
    throws Exception {
  // For decoding preview frame, expect some calls.
  final AnimatedDrawableBackend mockAnimatedDrawableBackend =
      createAnimatedDrawableBackendMock(2);

  when(mMockAnimatedDrawableBackendProvider.get(
          any(AnimatedImageResult.class), isNull(Rect.class)))
      .thenReturn(mockAnimatedDrawableBackend);

  when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG))
      .thenReturn(CloseableReference.of(mockBitmap1, FAKE_BITMAP_RESOURCE_RELEASER))
      .thenReturn(CloseableReference.of(mockBitmap2, FAKE_BITMAP_RESOURCE_RELEASER));
  AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
  PowerMockito.whenNew(AnimatedImageCompositor.class)
      .withAnyArguments()
      .thenReturn(mockCompositor);

  ImageDecodeOptions imageDecodeOptions =
      ImageDecodeOptions.newBuilder()
          .setDecodePreviewFrame(true)
          .setDecodeAllFrames(true)
          .build();

  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeGif(
              encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockGifImage, imageResult.getImage());
  assertNotNull(imageResult.getDecodedFrame(0));
  assertNotNull(imageResult.getDecodedFrame(1));
  assertNotNull(imageResult.getPreviewBitmap());

  // Should not have interacted with these.
  verify(mMockAnimatedDrawableBackendProvider)
      .get(any(AnimatedImageResult.class), isNull(Rect.class));
  verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
  verify(mMockBitmapFactory, times(2)).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
  verifyNoMoreInteractions(mMockBitmapFactory);
  verify(mockCompositor).renderFrame(0, mockBitmap1);
  verify(mockCompositor).renderFrame(1, mockBitmap2);
}
 
Example #15
Source File: CanViewPagerActivity.java    From CanPhotos with Apache License 2.0 3 votes vote down vote up
private void handleAnimateBitmap(CloseableAnimatedImage animatedImage, int position) {

        AnimatedDrawableFactory animatedDrawableFactory =
                Fresco.getImagePipelineFactory().getAnimatedDrawableFactory();
        AnimatedDrawable drawable =
                animatedDrawableFactory.create(animatedImage.getImageResult());

        Bitmap bitmap = drawable2Bitmap(drawable);
        map.put(position, bitmap);


    }