关于高德地图,自定义Marker使用自己的XMl,Fresco加载图片流程

因为高德地图的marker在设置自定的view时将view转换成图片,所以不能使用网络图片,这时就要我们自己处理。(ios可以android就要自己处理)大概思路就是,先读取缓存中是否有此图片,有则直接设置到marker的view中的imageview中,如果没有就要监听图片加载流程,图片加载完毕时,在设置一遍view到marker中,Fresco加载图片代码如下。

 vw = LayoutInflater.from(context).inflate(R.layout.view_mark_res, null);
            //判断缓存中是否存在
            boolean isCacheInDisk = Fresco.getImagePipelineFactory().getMainFileCache().hasKey(new SimpleCacheKey(tu));
            if (isCacheInDisk) {//如果存在直接拿出数据
                BinaryResource resource = ImagePipelineFactory.getInstance()
                        .getMainFileCache().getResource(new SimpleCacheKey(tu));
                if (null != resource) {
                    File file = ((FileBinaryResource) resource).getFile();//此处会有FileBinaryResource.getFile()' on a null object  BUG
                    bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    ((ImageView) vw.findViewById(R.id.iv_img)).setImageBitmap(bitmap);
                } else {
                    getFrescoCacheBitmap(Uri.parse(tu), context);
                }
            } else
                getFrescoCacheBitmap(Uri.parse(tu), context);

 private void getFrescoCacheBitmap(Uri uri, final Context context) {
        // final Bitmap frescoTepBm;
        ImageRequest imageRequest = ImageRequestBuilder
                .newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .build();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource>
                dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
        dataSource.subscribe(new BaseBitmapDataSubscriber() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onNewResultImpl(Bitmap b) {
                bitmap = b;
                View vw = LayoutInflater.from(context).inflate(R.layout.view_mark_res, null);
                if (null != marker) {
                    ((ImageView) vw.findViewById(R.id.iv_img)).setImageBitmap(b);
                    if (rt == 2)
                        vw.findViewById(R.id.iv_play).setVisibility(View.VISIBLE);
                    marker.setIcon(BitmapDescriptorFactory.fromView(vw));
                }
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
                LogUtils.e(TAG, " onFailureImp");
            }
        }, CallerThreadExecutor.getInstance());
    }


你可能感兴趣的:(android开发,高德地图使用)