Android added webp support in version 4.0 and improved it in 4.2.1:
4.0+ (Ice Cream Sandwich): basic webp support
4.2.1+ (Jelly Beam MR1): support for transparency and losless wepb
Fresco handles webp images by default if the OS supports it. So you can use webp with 4.0+ and trasparency and losless webps from 4.2.1.
Fresco also supports webp for older OS versions. The only thing you need to do is add thewebpsupportlibrary to your dependencies. So if you want to use webps on Gingerbread just add the following line to your gradle build file:
compile ‘com.facebook.fresco:webpsupport:1.3.0’
/** Performs the decode synchronously. */privatevoiddoDecode(EncodedImageencodedImage,@Statusintstatus){if(isFinished()||!EncodedImage.isValid(encodedImage)){return;}finalStringimageFormatStr;ImageFormatimageFormat=encodedImage.getImageFormat();if(imageFormat!=null){imageFormatStr=imageFormat.getName();}else{imageFormatStr="unknown";}finalStringencodedImageSize;finalStringsampleSize;finalbooleanisLast=isLast(status);finalbooleanisLastAndComplete=isLast&&!statusHasFlag(status,IS_PARTIAL_RESULT);finalbooleanisPlaceholder=statusHasFlag(status,IS_PLACEHOLDER);if(encodedImage!=null){encodedImageSize=encodedImage.getWidth()+"x"+encodedImage.getHeight();sampleSize=String.valueOf(encodedImage.getSampleSize());}else{// We should never be hereencodedImageSize="unknown";sampleSize="unknown";}finalStringrequestedSizeStr;finalResizeOptionsresizeOptions=mProducerContext.getImageRequest().getResizeOptions();if(resizeOptions!=null){requestedSizeStr=resizeOptions.width+"x"+resizeOptions.height;}else{requestedSizeStr="unknown";}try{longqueueTime=mJobScheduler.getQueuedTime();longdecodeDuration=-1;StringimageUrl=encodedImage.getEncodedCacheKey().getUriString();intlength=isLastAndComplete||isPlaceholder?encodedImage.getSize():getIntermediateImageEndOffset(encodedImage);QualityInfoquality=isLastAndComplete||isPlaceholder?ImmutableQualityInfo.FULL_QUALITY:getQualityInfo();mProducerListener.onProducerStart(mProducerContext.getId(),PRODUCER_NAME);CloseableImageimage=null;try{longnowTime=System.currentTimeMillis();image=mImageDecoder.decode(encodedImage,length,quality,mImageDecodeOptions);decodeDuration=System.currentTimeMillis()-nowTime;}catch(Exceptione){Map<String,String>extraMap=getExtraMap(image,imageUrl,queueTime,decodeDuration,quality,isLast,imageFormatStr,encodedImageSize,requestedSizeStr,sampleSize);mProducerListener.onProducerFinishWithFailure(mProducerContext.getId(),PRODUCER_NAME,e,extraMap);handleError(e);return;}Map<String,String>extraMap=getExtraMap(image,imageUrl,queueTime,decodeDuration,quality,isLast,imageFormatStr,encodedImageSize,requestedSizeStr,sampleSize);mProducerListener.onProducerFinishWithSuccess(mProducerContext.getId(),PRODUCER_NAME,extraMap);handleResult(image,status);}finally{EncodedImage.closeSafely(encodedImage);}}
publicinterfacePlatformDecoder{/** * Creates a bitmap from encoded bytes. Supports JPEG but callers should use {@link * #decodeJPEGFromEncodedImage} for partial JPEGs. * * @param encodedImage the reference to the encoded image with the reference to the encoded bytes * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded * Bitmap * @return the bitmap * @throws TooManyBitmapsException if the pool is full * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated */CloseableReference<Bitmap>decodeFromEncodedImage(finalEncodedImageencodedImage,Bitmap.ConfigbitmapConfig);/** * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image. * * @param encodedImage the reference to the encoded image with the reference to the encoded bytes * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded * Bitmap * @param length the number of encoded bytes in the buffer * @return the bitmap * @throws TooManyBitmapsException if the pool is full * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated */CloseableReference<Bitmap>decodeJPEGFromEncodedImage(EncodedImageencodedImage,Bitmap.ConfigbitmapConfig,intlength);}getBitmapFactoryOptions获取BitmapFactory.OptionsdecodeByteArrayAsPurgeable获取bitmappinBitmap真正的decode
/*
* Called when a producer successfully finishes processing current unit of work.
* @param extraMap Additional parameters about the producer. This map is immutable and will
* throw an exception if attempts are made to modify it.
/
void onProducerFinishWithSuccess(String requestId, String producerName, @Nullable Map<String, >String> extraMap);