如何从网络加载图像作为 android 中的标记图标?
How to load image from Network as marker icon in android?
我需要从 URL 为标记加载图像。在从 URL 加载图像之前,我需要显示默认值 image.And 我的地图页面将如下所示:
标记背景图片为白色,其中的图标(例如:包和汽车)将来自网络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 等任何图像加载库来执行上述过程吗?因此该图像将保留在缓存中。
请指教。谢谢。
试试Picaso。
这是一个库,加载图像和维护缓存非常容易。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
关于:
But if i use the above method means, marker rendering is somewhat slow
这个原因可能与图像大小有关。尝试压缩图像以减少一些字节。这将使您的加载程序更快。
我需要从 URL 为标记加载图像。在从 URL 加载图像之前,我需要显示默认值 image.And 我的地图页面将如下所示:
标记背景图片为白色,其中的图标(例如:包和汽车)将来自网络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 等任何图像加载库来执行上述过程吗?因此该图像将保留在缓存中。
请指教。谢谢。
试试Picaso。
这是一个库,加载图像和维护缓存非常容易。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
关于:
But if i use the above method means, marker rendering is somewhat slow
这个原因可能与图像大小有关。尝试压缩图像以减少一些字节。这将使您的加载程序更快。