2

Picasso那里存在RequestHandler类。我可以将自定义添加RequestHandlersPicasso.

如何做到这一点Glide

例如,我希望以下 URI 可以由自定义处理RequestHandler"appicon:custom_data_to_interprete_manually"

编辑-到目前为止我所拥有的

    public class GlideConfiguration implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // Apply options to the builder here.
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(CustomModelParams.class, CustomModelParams.class, new CustomFactory());
    }

    class CustomModelParams
    {
        final String data;

        public CustomModelParams(String data)
        {
            this.data = data;
        }

        public String getId()
        {
            return data;
        }
    }

    class CustomFactory implements ModelLoaderFactory<CustomModelParams, CustomModelParams>
    {
        @Override
        public ModelLoader<CustomModelParams, CustomModelParams> build(Context context, GenericLoaderFactory loaderFactory) {
            return new CustomModelLoader();
        }

        @Override
        public void teardown() {
        }
    }

    class CustomModelLoader implements ModelLoader<CustomModelParams, CustomModelParams>
    {
        public CustomModelLoader() {
            super();
        }

        @Override
        public DataFetcher<CustomModelParams> getResourceFetcher(final CustomModelParams model, int width, int height)
        {
            return new DataFetcher<CustomModelParams>()
            {
                @Override
                public CustomModelParams loadData(Priority priority) throws Exception { return model; }
                @Override
                public void cleanup() { }
                @Override
                public String getId() { return model.getId(); }
                @Override
                public void cancel() { }
            };
        }
    }

    class CustomBitmapDecoder implements ResourceDecoder<CustomModelParams, Bitmap>
    {
        private final Context context;

        public CustomBitmapDecoder(Context context)
        {
            this.context = context;
        }

        @Override
        public Resource<Bitmap> decode(CustomModelParams source, int width, int height) throws IOException
        {
            BitmapPool pool = Glide.get(context).getBitmapPool();
            Bitmap bitmap = pool.getDirty(width, height, Bitmap.Config.ARGB_8888);
            if (bitmap == null) {
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            }

            // TODO
            // create custom bitmap from CustomModelParams!!!

            return BitmapResource.obtain(bitmap, pool);
        }

        @Override
        public String getId()
        {
            return CustomBitmapDecoder.class.getName();
        }
    }
}

问题

  • 我如何将这些类链接在一起?解码器必须以某种方式与新模型链接
  • 如何定义我的自定义加载器可以处理请求?我必须以某种方式确定我得到的 url 是否可以被这个加载器处理......
4

2 回答 2

0

您可以使用ModelLoaders。有关自定义 ModelLoader 的示例,请参阅下载自定义尺寸 wiki 。请注意,您需要注册一个处理特定数据模型的 ModelLoader,而不是 BaseGlideUrlLoader。

于 2015-09-10T23:25:33.327 回答
0

我为我的项目实现了非常相似的代码(它从包管理器加载其他应用程序图标)和

@Override
public void registerComponents(Context context, Glide glide) {
    glide.register(MyDataModel.class, Bitmap.class, new MyUrlLoader.Factory());
}

对我不起作用。所以为了制作自定义加载的作品,我只使用 Glide Builder

private final GenericRequestBuilder<MyDataModel, Bitmap, Bitmap, Bitmap> mGlideBuilder;

    mGlideBuilder = Glide.with(mContext)
            .using(new MyUrlLoader(mContext), Bitmap.class)
            .from(MyDataModel.class)
            .as(Bitmap.class)
            .decoder(new MyBitmapDecoder())
            .diskCacheStrategy(DiskCacheStrategy.NONE);

    mGlideBuilder.load(entry).into(holder.icon);

并声明MyBitmapDecoderMyUrlLoader

    public class MyBitmapDecoder implements ResourceDecoder<Bitmap, Bitmap> {

    public class MyUrlLoader implements ModelLoader<MyDataModel, Bitmap> {
于 2016-02-08T00:02:23.713 回答