【问题标题】:How to mark a given Latitude and Longtitude into still image map如何将给定的纬度和经度标记到静止图像地图中
【发布时间】:2011-09-07 12:56:26
【问题描述】:

@Dan S,在这篇文章中给出了以下声明 -> display a still image map

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

如何为指标创建第二个 ImageView?它应该是透明的 imageview 以及指示图像。

更新: 我在这里询问如何将我的图像映射 ImageView 和透明指示器 ImageView 重叠以标记当前位置。

【问题讨论】:

  • 我的道歉,我的意思是关于如何重叠第二个 ImageView 以通过给定的坐标将当前位置标记到图像地图上,而不是关于如何创建 ImageView。我有从现实生活坐标到像素的转换类。问题在于如何重叠两个 ImageView。让我更新我的帖子。

标签: android android-imageview


【解决方案1】:

ImageView 默认是透明的。 您的指示图像资源应具有透明背景(例如透明 .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 更新问题后,这里有一个更新的答案: 我能想到有两个选项可以实现将一个图像视图定位在另一个图像视图之上:

  1. 使用 LayoutParams 并将边距设置为指示器图像视图的位置
  2. 在地图 bmp 的画布上绘制指示器图像视图

我个人更喜欢第一个选项,因为未来的更改不会迫使您重新绘制指标。

这是一个演示选项 (1) 的课程:

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);
    }
}

【讨论】:

  • 是的,你是对的。我更新我的帖子以添加更多信息。我正在询问如何重叠两个图像视图以标记我当前的位置。
  • 请问扩展RelativeLayout的设置(如Layout Parameters)?
  • @eros - 如果您询问应该用于 MyMapView 类的 LayoutParams,那么这取决于您需要的整体布局。您可以将其添加到另一个 ViewGroup 并使用: new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
  • 酷~明白了。让我在这里声明,我的最终目标是 (1) 标记当前 (2) 标记目的地 (3) 在放大/缩小时支持 1&2。我会把它作为另一个问题发布,你也看看吗...
猜你喜欢
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2013-04-17
相关资源
最近更新 更多