如何从网络加载图像作为android中的标记图标?

时间:2017-08-23 12:22:24

标签: android google-maps-markers

我需要从网址加载标记的图片。在从URL加载图像之前,我需要显示默认图像。我的地图页面如下所示:

enter image description here

标记背景图像为白色,其中的图标(例如:行李和汽车)将来自网络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等任何Image加载库来完成上述过程吗?这样图像将在Cache中维护。

请指教。谢谢。

1 个答案:

答案 0 :(得分:2)

尝试Picaso

这是一个库,如何轻松加载图像和维护缓存。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

关于:

  

但是如果我使用上面的方法意味着,标记渲染有点慢

原因可能与图像尺寸有关。尝试压缩图像以减少一些字节。这将使您的装载机更快。

Online compressor for iamgens