如何将图像从毕加索加载到子采样比例图像视图,请问任何人都可以简化这一点吗?



我想通过毕加索库将图像从URL加载到android工作室中的子采样比例图像视图。

我尝试过毕加索解码器和毕加索区域解码器,但它在okhttp3downloader客户端构造函数上给出错误。

public class PicassoRegionDecoder implements ImageRegionDecoder {
private OkHttpClient client;
private BitmapRegionDecoder decoder;
private final Object decoderLock = new Object();
public PicassoRegionDecoder (OkHttpClient client) {
this.client = client;
}
@Override
public Point init(Context context, Uri uri) throws Exception {
OkHttp3Downloader downloader=new OkHttp3Downloader(client);
//OkHttpDownloader downloader = new OkHttpDownloader(client);
InputStream inputStream = downloader.load(uri, 0).getInputStream();
this.decoder = BitmapRegionDecoder.newInstance(inputStream, false);
return new Point(this.decoder.getWidth(), this.decoder.getHeight());
}
@Override
public Bitmap decodeRegion(Rect rect, int sampleSize) {
synchronized(this.decoderLock) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = this.decoder.decodeRegion(rect, options);
if(bitmap == null) {
throw new RuntimeException("Region decoder returned null bitmap - image format may not be supported");
} else {
return bitmap;
}
}
}
@Override
public boolean isReady() {
return this.decoder != null && !this.decoder.isRecycled();
}
@Override
public void recycle() {
this.decoder.recycle();
}
}

无法解析构造函数 OkHttpDownloader(客户端(

答案

子采样比例图像视图是一个"旧"帖子。

您必须小心导入,这些正在工作:

implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp:okhttp:2.1.0'

现在,如果您想使用最新的库,请将代码片段更改为:

@Override
public Point init(Context context, Uri uri) throws Exception {
OkHttp3Downloader downloader = new OkHttp3Downloader(client);
okhttp3.Request request = new Request.Builder().url(uri.toString()).build();
InputStream inputStream = downloader.load(request).body().byteStream();
this.decoder = BitmapRegionDecoder.newInstance(inputStream, false);
return new Point(this.decoder.getWidth(), this.decoder.getHeight());
}

请参考DAVEMORRISSEY(THE DEV(的帖子

这里https://gist.github.com/davemorrissey/e2781ba5b966c9e95539

并阅读评论

相关内容

最新更新