【问题标题】:create custom marker icons on runtime using android xml使用 android xml 在运行时创建自定义标记图标
【发布时间】:2014-07-26 08:12:09
【问题描述】:

我正在开发一个 Android 应用程序,我需要从其 URL 下载图像,从中创建位图,然后将其作为图标显示在地图上。到目前为止,我已成功完成在地图上动态显示位图,但我需要将图像放置在某些背景上。像这样显示在这里How to create a custom-shaped bitmap marker with Android map API v2? 任何示例或建议都会有所帮助。 谢谢...:)

【问题讨论】:

    标签: android google-maps-markers google-maps-android-api-2


    【解决方案1】:

    我得到了上述问题的答案 我从视图中创建了一个位图,然后将其放置在标记上。我的 xml 在下面

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <ImageView
        android:id="@+id/img_id"
        android:layout_width="@dimen/custom_profile_image"
        android:layout_height="@dimen/custom_profile_image"
        android:contentDescription="@string/content"
        android:scaleType="centerCrop" />
        </RelativeLayout>
    

    在 xml 之上膨胀

        View viewMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.custom_marker_layout, null);
        ImageView myImage = (ImageView) viewMarker.findViewById(R.id.img_id);
    

    在运行时从 url 下载的 imageview 上设置 Imagebitmap

        myImage.setImageBitmap(Imagebitmap);
    

    现在将视图对象传递给方法并从中返回位图。像这样

        Bitmap bmp=createDrawableFromView(context,viewMarker);
    
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude)).title(title)
                .snippet(snippet)
                .icon(BitmapDescriptorFactory.fromBitmap(bmp)));
    

    现在您可以在任何地方使用返回的位图,那就是您可以使用 xmls 制作自定义标记图标。

        public static Bitmap createDrawableFromView(Context context, View view) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(displayMetrics);
        view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels,
                displayMetrics.heightPixels);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
    

    它对我有用。谢谢!!!

    【讨论】:

      【解决方案2】:

      可能有更好的解决方案,但不妨试试这个:

      将你需要的背景放入你的drawable文件夹并加载位图:

      Bitmap marker = getResources().getDrawable(R.drawable.marker_background);
      

      用你下载的图片编辑一个副本(只是伪的,不知道是不是这样):

          int xOffset = 20;
          int yOffset = 50;
          int xPosition = xOffset;
          int yPosition = yOffset;
          int numberOfPixels = image.getHeight()*image.getWidth();
          int[] pixels = new int[numberOfPixels];
          image.getPixels(pixels,0,0,0,0,image.getWidth(), image.getHeight());
      
      
          for (int x = 0; x<numberOfPixels;x++){
              marker.setPixel(xPosition,yPosition,pixels[x]);
              if((xPosition-xOffset)%image.getWidth()==0){
                  yPosition+=1;
                  xPosition=xOffset;
              }else{
                  xPosition++;
              }
      
          }
      
          MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(marker));
      

      【讨论】:

        猜你喜欢
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        相关资源
        最近更新 更多