glideデータソースをカスタマイズして、socketで画像を取得します.

17435 ワード

1、GlideModuleを作成します.目的はカスタムデータオブジェクトを登録することです.glideを使う時に、何度もusing()メソッドを呼び出す必要がないです.
public class CustomGlideModule implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        ViewTarget.setTagId(R.id.glide_tag_id); //     get/set tag id,    View   
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); //           
        builder.setMemoryCache(new LruResourceCache(10 * 1024 * 1024));
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        //      ImageFidLoader
        glide.register(ImageFid.class, InputStream.class, new ImageFidLoader.Factory());
    }
}
2、リストファイルを修正して、ユーザー定義のglidemoduleを有効にします.
アプリで設定する
<meta-data
    android:name="com.glide.CustomGlideModule"
    android:value="GlideModule" />
3、カスタムデータソースオブジェクト
public class ImageFid {
    // fid
    private String mFid;//   id

    public ImageFid(String fid) {
        mFid = fid;
    }

    //     equals hashCode,        ImageFid
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ImageFid imageFid = (ImageFid) o;
        if (mFid == null) return true;
        return mFid.equals(imageFid.mFid);

    }

    @Override
    public int hashCode() {
        if (!TextUtils.isEmpty(mFid)) {
            return mFid.hashCode();
        } else {
            return 0;
        }
    }

    public String getFid() {
        if (mFid == null) return "";
        return mFid;
    }


}
4,  StreamModelLoader,      DataFetcher, DataFetcher          
public class ImageFidLoader implements StreamModelLoader {

    private final ModelCache mModelCache;

    public ImageFidLoader() {
        this(null);
    }

    public ImageFidLoader(ModelCache modelCache) {
        mModelCache = modelCache;
    }

    @Override
    public DataFetcher getResourceFetcher(ImageFid model, int width, int height) {
        if(model==null) return null;
        ImageFid imageFid = model;
        //       ImageFid,ImgeFid   equals() hashCode()  
        //    ImgeFid   url,         
        if (mModelCache != null) {
            imageFid = mModelCache.get(model, 0, 0);
            if (imageFid == null) {
                mModelCache.put(model, 0, 0, model);
                imageFid = model;
            }
        }
        return new ImageFidFetcher(imageFid);
    }

    // ModelLoader  ,  Glide     ModelLoader    
    public static class Factory implements ModelLoaderFactory {
        //   
        private final ModelCache mModelCache = new ModelCache<>(500);

        @Override
        public ModelLoader build(Context context, GenericLoaderFactory factories) {
            //   ImageFidLoader  
            return new ImageFidLoader(mModelCache);
        }

        @Override
        public void teardown() {

        }
    }

}
、作成
ImageFidFetch類、この中
loadDataはネットワーク要求の一部コードで、それを置き換えればいいです.
public class ImageFidFetcher implements DataFetcher {

    //            
    private volatile boolean mIsCanceled = false;
    private final ImageFid mImageFid;
    private InputStream mInputStream;

    public ImageFidFetcher(ImageFid imageFid) {
        mImageFid = imageFid;
    }

    /**
     *         ,          , Glide                ,           
     *
     * @param priority
     * @return
     * @throws Exception
     */
    @Override
    public InputStream loadData(Priority priority) throws Exception {
        if (mImageFid == null) {
            return null;
        }
        if (mIsCanceled) {
            return null;
        }
        TransObject obj = new TransObject();
        obj.ServiceName = Constant.ServerName;
        obj.FunctionName = Constant.Image;
        obj.JSONParameter = mImageFid.getFid();
        if (CallerApplication._isConnected) {
            try {
                Log.i("    :  ID", (new Gson()).toJson(obj));
                String ret = CallerApplication.getConnection().AS_TransTrunk(obj);
                ObjModeBean data = GsonConvertUtil.convertObj(ImageBean.class, ret);
                Log.i("    :", ret);
                Bitmap bmp = BitmapUtil.getInstance().stringtoBitmap(data.Data.fileStr);
                mInputStream = BitmapUtil.getInstance().Bitmap2InputStream(bmp);
                return mInputStream;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     *         , Glide   {@link #loadData(Priority)}      ,         
     */
    @Override
    public void cleanup() {
        if (mInputStream != null) {
            try {
                mInputStream.close();
            } catch (IOException e) {
                //e.printStackTrace();
            } finally {
                mInputStream = null;
            }
        }
    }

    /**
     *  UI     ,           id
     *
     * @return
     */
    @Override
    public String getId() {
        return mImageFid.getFid();
    }

    /**
     *  UI     ,      
     */
    @Override
    public void cancel() {
        mIsCanceled = true;
    }
}