Android 如何在静止图像地图中标注给定的纬度和经度

Android 如何在静止图像地图中标注给定的纬度和经度,android,android-imageview,Android,Android Imageview,@Dan S在这篇文章中发表了以下声明-> 然后使用第二个图像视图(用于指示器)在顶部和顶部绘制它 将其移动到屏幕上的正确位置,并确保 此视图中的背景是透明的,并且是 你的指标 如何为指示器创建第二个图像视图?它应该是透明的图像视图以及指示器图像 更新: 我在这里询问如何重叠图像地图ImageView和透明指示器ImageView以标记当前位置。默认情况下,An是透明的。 指示器图像资源应具有透明背景(例如,transparent.PNG图像) 只需将ImageView的imageResourc

@Dan S在这篇文章中发表了以下声明->

然后使用第二个图像视图(用于指示器)在顶部和顶部绘制它 将其移动到屏幕上的正确位置,并确保 此视图中的背景是透明的,并且是 你的指标

如何为指示器创建第二个图像视图?它应该是透明的图像视图以及指示器图像

更新: 我在这里询问如何重叠图像地图ImageView和透明指示器ImageView以标记当前位置。默认情况下,An是透明的。 指示器图像资源应具有透明背景(例如,transparent.PNG图像)

只需将ImageView的imageResource或backgroundResource设置为PNG文件

因此,创建ImageView的代码如下所示:

ImageView myImageView = new ImageView(context);
myImageView.setBackgroundColor(0x0); // not needed - it's the default
myImageView.setImageResource(R.drawable.indicator_icon);
但同样,这是默认设置,您仍然必须确保图像的背景是透明的,否则ImageView的背景将无关紧要

更新:在@eros更新问题之后,这里是更新的答案: 我可以想到,有两种方法可以实现将一个图像视图置于另一个图像视图之上:

  • 使用LayoutParams并将边距设置为指示器imageview的位置
  • 在地图的画布上绘制指示器图像视图
  • 就我个人而言,我更喜欢第一个选项,因为未来的更改不会迫使您重新绘制指示器

    下面是一个课堂演示选项(1):


    我的道歉是,我的意思是如何重叠第二个ImageView,根据给定坐标标记当前位置,而不是如何创建ImageView。我有我的转换类从现实生活中的坐标到像素。问题在于如何重叠两个ImageView。让我来更新我的帖子。是的,你说得对。我更新我的帖子以添加更多信息。我正在询问如何重叠两个imageview以标记我的当前位置。我可以询问extended RelativeLayout(例如布局参数)的设置吗?@eros-如果您询问MyMapView类应使用的布局参数,则这取决于您需要的总体布局。您可以将其添加到另一个视图组并使用:new LayoutParams(LayoutParams.FILL\u PARENT,LayoutParams.FILL\u PARENT)Cool~明白了。让我在此声明,我的最终目标是(1)标记当前(2)标记目标(3)在放大/缩小时支持1和2。我会把它作为另一个问题贴出来,你能不能也检查一下。。。
    public class MyMapView extends RelativeLayout {
        private ImageView mBackImageView;
        private ImageView mIndicatorImageView;
    
        public MyMapView(Context context) {
            super(context);
    
            mBackImageView = new ImageView(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            mBackImageView.setImageResource(R.drawable.image1);
            mBackImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            addView(mBackImageView, params);
    
            mIndicatorImageView = new ImageView(context);
            RelativeLayout.LayoutParams indParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            mIndicatorImageView.setImageResource(R.drawable.image2);
            addView(mIndicatorImageView, indParams);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            centerIndicatorPosition();
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            centerIndicatorPosition();
        }
    
        // @eros: this's the part you want. this method just centers the indicator view
        // but if you have the relative position like you say, use it for the margins
        private void centerIndicatorPosition() {
            int xPos = (getMeasuredWidth() - mIndicatorImageView.getMeasuredWidth()) / 2;
            int yPos = (getMeasuredHeight() - mIndicatorImageView.getMeasuredHeight()) / 2;
            ((RelativeLayout.LayoutParams)mIndicatorImageView.getLayoutParams())
                .setMargins(xPos, yPos, 0, 0);
        }
    }