public class Png {
public Png(byte[] resource) {
this.resource = resource;
}
public final byte[] resource;
}
public class PngBitmapBytesTranscoder implements ResourceTranscoder<Bitmap, Png> {
private final Bitmap.CompressFormat compressFormat;
private final int quality;
public PngBitmapBytesTranscoder() {
this(Bitmap.CompressFormat.PNG, 100);
}
@SuppressWarnings("WeakerAccess")
public PngBitmapBytesTranscoder(@NonNull Bitmap.CompressFormat compressFormat, int quality) {
this.compressFormat = compressFormat;
this.quality = quality;
}
@Nullable
@Override
public Resource<Png> transcode(
@NonNull Resource<Bitmap> toTranscode, @NonNull Options options) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
toTranscode.get().compress(compressFormat, quality, os);
toTranscode.recycle();
Png png = new Png(os.toByteArray());
return new PngResource(png);
}
}
public class PngResource implements Resource<Png> {
private final Png bytes;
public PngResource(Png bytes) {
this.bytes = Preconditions.checkNotNull(bytes);
}
@NonNull
@Override
public Class<Png> getResourceClass() {
return Png.class;
}
@NonNull
@Override
public Png get() {
return bytes;
}
@Override
public int getSize() {
return bytes.resource.length;
}
@Override
public void recycle() {
}
}
@GlideModule
public class SimpleGlideModule extends AppGlideModule {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
super.registerComponents(context, glide, registry);
registry.register(Bitmap.class, Png.class, new PngBitmapBytesTranscoder());
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
}
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
调用方式
private ImageView mIv2;
Glide.with(mIv2)
.as(Png.class)
.load("https://akatweb.mapfarm.com/arcgis/rest/services/BaseMapUpdate/MapServer/tile/7/56/102")
.addListener(new RequestListener<Png>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Png> target, boolean isFirstResource) {
System.out.println("加载失败=======PNG====="+e);
return false;
}
@Override
public boolean onResourceReady(Png resource, Object model, Target<Png> target, DataSource dataSource, boolean isFirstResource) {
System.out.println("加载成功==========--PNG----==");
runOnUiThread(new Runnable() {
@Override
public void run() {
BitmapFactory.Options options1 = new BitmapFactory.Options();
// options1.inPreferredConfig = Bitmap.Config.ARGB_8888;
// options1.inPreferredConfig = Bitmap.Config.RGB_565;
// Bitmap bitmap1 = BitmapFactory.decodeByteArray(resource.resource, 0 , resource.resource.length, options1);
Bitmap bitmap1 = BitmapFactory.decodeByteArray(resource.resource, 0 , resource.resource.length);
mIv2.setImageBitmap(bitmap1);
}
});
return false;
}
})
.submit();
瓦片图是png格式,glide默认用jpg去转码,导致加载瓦片图透明区域变黑,如图所示,
问题,怎么能智能按需转换呢?比如瓦片图是jpg,就用jpg转换,瓦片图是png就用png转换~ 看glide看的头大