use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project boxing by Bilibili.
the class BoxingFrescoLoader method createDrawableFromFetchedResult.
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);
}
use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.
the class AnimatedImageFactoryWebPImplTest method testCreateWithDecodeAlFrames.
@Test
public void testCreateWithDecodeAlFrames() throws Exception {
WebPImage mockWebPImage = mock(WebPImage.class);
Bitmap mockBitmap1 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
Bitmap mockBitmap2 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
// Expect a call to WebPImage.create
TrivialPooledByteBuffer byteBuffer = createByteBuffer();
when(mWebPImageMock.decode(byteBuffer.getNativePtr(), byteBuffer.size())).thenReturn(mockWebPImage);
when(mockWebPImage.getWidth()).thenReturn(50);
when(mockWebPImage.getHeight()).thenReturn(50);
// 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);
}
use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.
the class AnimatedImageFactoryWebPImplTest method testCreateDefaults.
@Test
public void testCreateDefaults() {
WebPImage mockWebPImage = mock(WebPImage.class);
// Expect a call to WebPImage.create
TrivialPooledByteBuffer byteBuffer = createByteBuffer();
when(mWebPImageMock.decode(byteBuffer.getNativePtr(), byteBuffer.size())).thenReturn(mockWebPImage);
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);
}
use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project DevRing by LJYcoder.
the class FrescoManager method getBitmap.
@Override
public void getBitmap(Context context, String url, final ImageListener<Bitmap> imageListener) {
// 参考自https://github.com/hpdx/fresco-helper/blob/master/fresco-helper/src/main/java/com/facebook/fresco/helper/ImageLoader.java
Uri uri = Uri.parse(url);
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
ImageRequest imageRequest = builder.build();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableImage> imageReference = dataSource.getResult();
if (imageReference != null) {
final CloseableReference<CloseableImage> closeableReference = imageReference.clone();
try {
CloseableImage closeableImage = closeableReference.get();
// 动图处理
if (closeableImage instanceof CloseableAnimatedImage) {
AnimatedImageResult animatedImageResult = ((CloseableAnimatedImage) closeableImage).getImageResult();
if (animatedImageResult != null && animatedImageResult.getImage() != null) {
int imageWidth = animatedImageResult.getImage().getWidth();
int imageHeight = animatedImageResult.getImage().getHeight();
Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, bitmapConfig);
animatedImageResult.getImage().getFrame(0).renderFrame(imageWidth, imageHeight, bitmap);
if (imageListener != null) {
imageListener.onSuccess(bitmap);
}
}
} else // 非动图处理
if (closeableImage instanceof CloseableBitmap) {
CloseableBitmap closeableBitmap = (CloseableBitmap) closeableImage;
Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
// https://github.com/facebook/fresco/issues/648
final Bitmap tempBitmap = bitmap.copy(bitmap.getConfig(), false);
if (imageListener != null) {
imageListener.onSuccess(tempBitmap);
}
}
}
} finally {
imageReference.close();
closeableReference.close();
}
}
}
@Override
public void onFailureImpl(DataSource dataSource) {
Throwable throwable = dataSource.getFailureCause();
if (imageListener != null) {
imageListener.onFail(throwable);
}
}
}, UiThreadImmediateExecutorService.getInstance());
}
use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.
the class ExperimentalBitmapAnimationDrawableFactory method createDrawable.
@Override
public AnimatedDrawable2 createDrawable(CloseableImage image) {
CloseableAnimatedImage closeable = ((CloseableAnimatedImage) image);
AnimatedImage animatedImage = closeable.getImage();
return new AnimatedDrawable2(createAnimationBackend(Preconditions.checkNotNull(closeable.getImageResult()), animatedImage != null ? animatedImage.getAnimatedBitmapConfig() : null));
}
Aggregations