因爲高德地圖的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<CloseableReference<CloseableImage>>
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());
}