Android P ImageDecoder 封装工具类

本文介绍了Android P中新增的ImageDecoder类,并展示了如何封装成工具类以简化图片加载过程。包括基本用法、加载监听、处理未完全加载的图片情况,以及提供的静态代理类实现。

Android P ImageDecoder 封装工具类

翻阅安卓版本信息时,发现在Android P中新增了一个用于替代BitmapFactory的类ImageDecoder类,稍微研究了一下,感觉很有趣,便尝试着写了个工具类出来丰富我的个人开发框架

基本用法

// 测试方法 java
void test(File file){
   
   
    ImageDecoder.Source source = ImageDecoder.createSource(file);
    Drawable d = ImageDecoder.decodeDrawable(source);
    Bitmap b = ImageDecoder.decodeBitmap(source);
}
// 测试方法 kotlin
fun test(file: File){
   
   
    val source = ImageDecoder.createSource(file)
    val drawable = ImageDecoder.decodeDrawable(source)
    val bitmap = ImageDecoder.decodeBitmap(source)
}

上面的代码就是通过ImageDecoder加载一个本地图片文件的过程。很简单,过程基本就是先获取一个ImageDecoder.Source实例,再将该实例作为参数获取图片。该方法是耗时操作,不建议在主线程中使用。

加载监听

一般为了我们会为了一些原因在图片加载出来前进行一些操作,比如防止OOM而提前进行压缩,或者将图片进行一些处理。比如下面的官方例子就是在图片加载前对图片进行圆角化处理

//官方用到了java 1.8 的lambda表达式
void test(){
   
   
  Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
   
   
      decoder.setPostProcessor((canvas) -> {
   
   
              // This will create rounded corners.
              Path path = new Path();
              path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
              int width = canvas.getWidth();
              int height = canvas.getHeight();
              path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW);
              Paint paint = new Paint();
              paint.setAntiAlias(true);
              paint.setColor(Color.TRANSPARENT);
              paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
              canvas.drawPath(path, paint);
              return PixelFormat.TRANSLUCENT;
      });
  });
 }
<
package androidx.core.graphics; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.ImageDecoder; import android.graphics.ImageDecoder.ImageInfo; import android.graphics.ImageDecoder.Source; import android.graphics.drawable.Drawable; import androidx.annotation.RequiresApi; import androidx.core.graphics.ImageDecoderKt$.ExternalSyntheticApiModelOutline1; import kotlin.Metadata; import kotlin.Unit; import kotlin.jvm.functions.Function3; import kotlin.jvm.internal.Intrinsics; import org.jetbrains.annotations.NotNull; @Metadata(d1 = {"\u00000\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u001aU\u0010\u0000\u001a\u00020\u0001*\u00020\u00022C\b\u0004\u0010\u0003\u001a=\u0012\u0004\u0012\u00020\u0005\u0012\u0013\u0012\u00110\u0006¢\u0006\f\b\u0007\u0012\b\b\b\u0012\u0004\b\b(\t\u0012\u0013\u0012\u00110\u0002¢\u0006\f\b\u0007\u0012\b\b\b\u0012\u0004\b\b(\n\u0012\u0004\u0012\u00020\u000b0\u0004¢\u0006\u0002\b\fH‡\bø\u0001\u0000\u001aU\u0010\r\u001a\u00020\u000e*\u00020\u00022C\b\u0004\u0010\u0003\u001a=\u0012\u0004\u0012\u00020\u0005\u0012\u0013\u0012\u00110\u0006¢\u0006\f\b\u0007\u0012\b\b\b\u0012\u0004\b\b(\t\u0012\u0013\u0012\u00110\u0002¢\u0006\f\b\u0007\u0012\b\b\b\u0012\u0004\b\b(\n\u0012\u0004\u0012\u00020\u000b0\u0004¢\u0006\u0002\b\fH‡\bø\u0001\u0000‚\u0002\u0007\n\u0005\b™20\u0001¨\u0006\u000f"}, d2 = {"decodeBitmap", "Landroid/graphics/Bitmap;", "Landroid/graphics/ImageDecoder$Source;", "action", "Lkotlin/Function3;", "Landroid/graphics/ImageDecoder;", "Landroid/graphics/ImageDecoder$ImageInfo;", "Lkotlin/ParameterName;", "name", "info", "source", "", "Lkotlin/ExtensionFunctionType;", "decodeDrawable", "Landroid/graphics/drawable/Drawable;", "core-ktx_release"}, k = 2, mv = {1, 7, 1}, xi = 48) @SuppressLint({"ClassVerificationFailure"}) /* compiled from: ImageDecoder.kt */ public final class ImageDecoderKt { @RequiresApi(28) @NotNull public static final Bitmap decodeBitmap(@NotNull Source source, @NotNull Function3<? super ImageDecoder, ? super ImageInfo, ? super Source, Unit> function3) { Intrinsics.checkNotNullParameter(source, "<this>"); Intrinsics.checkNotNullParameter(function3, "action"); Bitmap m = ExternalSyntheticApiModelOutline1.m(source, new 1(function3)); Intrinsics.checkNotNullExpressionValue(m, "crossinline action: Imag…ction(info, source)\n }"); return m; } @RequiresApi(28) @NotNull public static final Drawable decodeDrawable(@NotNull Source source, @NotNull Function3<? super ImageDecoder, ? super ImageInfo, ? super Source, Unit> function3) { Intrinsics.checkNotNullParameter(source, "<this>"); Intrinsics.checkNotNullParameter(function3, "action"); Bitmap m = ExternalSyntheticApiModelOutline1.m(source, new 1(function3)); Intrinsics.checkNotNullExpressionValue(m, "crossinline action: Imag…ction(info, source)\n }"); return m; } }
最新发布
06-12
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值