如何将图像从毕加索加载到子采样比例图像视图。请任何人简化一下吗?

问题描述 投票:1回答:1

我想通过Picasso库从URL加载图像到android studio中的subsampling-scale-image-view。

我尝试过毕加索解码器和毕加索区域解码器,但在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(client)

java android picasso
1个回答
0
投票

ANSWER

子采样比例图像视图是“旧”帖子。

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

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的(开发人员)在这里发布

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

并阅读评论

© www.soinside.com 2019 - 2024. All rights reserved.