【发布时间】:2017-08-23 20:50:01
【问题描述】:
我需要从 URL 加载标记的图像。在从 URL 加载图像之前,我需要显示默认图像。我的地图页面如下所示:
标记背景图像为白色,其中的图标(例如:包和汽车)将来自网络 URL。
我已经通过使用 BitmapDescriptorFactory.fromBitmap 创建标记来实现相关输出。
if (isAdded()) {
marker = googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(setMarkerFromlayout(result, false, false), 200, 200)))
.position(pinLocation)
.title(parser.mapLocationList.get(pos).location_id)
.snippet("")
);
}
public static Bitmap createDrawableFromView(View v, int requestedWidth, int requestedHeight) {
// Because the view is never shown it does not have a size, but if shown don't resize it.
if (v.getMeasuredHeight() <= 0) {
// You may want to change these lines according to the behavior you need.
int specWidth = View.MeasureSpec.makeMeasureSpec(requestedWidth, View.MeasureSpec.AT_MOST);
int specHeight = View.MeasureSpec.makeMeasureSpec(requestedHeight, View.MeasureSpec.AT_MOST);
v.measure(specWidth, specHeight);
}
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
private RelativeLayout setMarkerFromlayout(Bitmap resultImage, boolean isClickable, boolean isFirst) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_map_custom_icon, null);
RelativeLayout layoutIcon = (RelativeLayout) view.findViewById(R.id.layout_icon);
ImageView imgBg = (ImageView) view.findViewById(R.id.bg_icon);
ImageView imgdefault = (ImageView) view.findViewById(R.id.default_icon);
if (isClickable) {
layoutIcon.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.green_round_bg));
}
imgBg.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.map_bg));
if(isFirst){
imgdefault.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.marker_default));
}
final ImageView imgMap = (ImageView) view.findViewById(R.id.img_map_icon);
if (!isFirst) {
imgMap.setImageBitmap(resultImage);
}
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imgMap.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.height = BDevice.getPixelFromDp(getActivity(), 30);
layoutParams.width = BDevice.getPixelFromDp(getActivity(), 30);
layoutParams.setMargins(BDevice.getPixelFromDp(getActivity(), 10), BDevice.getPixelFromDp(getActivity(), 10), BDevice.getPixelFromDp(getActivity(), 10), BDevice.getPixelFromDp(getActivity(), 20));
imgMap.setLayoutParams(layoutParams);
return layoutIcon;
}
layout_map_custom_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layout_icon"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/bg_icon"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/default_icon"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/img_map_icon" />
</RelativeLayout>
但是如果我使用上述方法,标记渲染会有点慢,而且图像也不会保存在缓存中。因此,每当我每次下载图像并设置为标记时退出应用程序。我们可以使用 Picasso、Fresco 或 Glide 等任何图像加载库来完成上述过程吗?这样该图像将保留在缓存中。
请指教。谢谢。
【问题讨论】:
标签: android google-maps-markers