如何将视图添加到mapView上的特定点

发布于 2024-11-18 01:46:42 字数 435 浏览 2 评论 0原文

当我的 MapView 上发生 onTouch 事件时,我希望我指定的布局绘制在用户触摸的位置上方。

下面是我开始的位置,我不知道如何实际将弹出视图放置在 x 和 y 上从事件中检索的值。你们有什么想法吗?

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

int X = (int)ev.getX();          
int Y = (int)ev.getY();

ViewGroup parent=(ViewGroup)map.getParent();
View popup=getLayoutInflater().inflate(R.id.popup, parent, false);

((ViewGroup)map.getParent()).addView(popup);

an When an onTouch event occurs on my MapView, i want layout i have specified to be drawn above the where the user has touched

The below is where i have started, i do not know how to actually place the popup view on the x and y values retrieved from the event. Do you guys have any ideas?

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

int X = (int)ev.getX();          
int Y = (int)ev.getY();

ViewGroup parent=(ViewGroup)map.getParent();
View popup=getLayoutInflater().inflate(R.id.popup, parent, false);

((ViewGroup)map.getParent()).addView(popup);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-11-25 01:46:42

您可以使用 x 和 y 的值设置 MapView.LayoutParams(),然后使用 LayoutParams 调用 MapView 的 addView

MapView.LayoutParams params = new MapView.LayoutParams(width, height, x, y, alignment);
mapView.addView(popup, params);

You can set the MapView.LayoutParams() with the values of x and y and then call the MapView's addView with the LayoutParams.

MapView.LayoutParams params = new MapView.LayoutParams(width, height, x, y, alignment);
mapView.addView(popup, params);
椵侞 2024-11-25 01:46:42

首先,您需要一个带有 X、Y 坐标的全局点。

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
    int X = (int)ev.getX();          
    int Y = (int)ev.getY();

    selectedPoint = new Point(X,Y);
    return true;
}

现在,您应该重写 onDraw() 方法来绘制弹出视图。有很多解决方案可以做到这一点,我用 StaticLayout 尝试过这个:

@Override
public void draw (Canvas canvas, MapView mapView, boolean shadow){
    super.draw(canvas, mapView, false); 
    if ( selectedPoint != null) {

        // here you should define PopupText, textPaint and width variables

        StaticLayout sl = new StaticLayout(popupText, textPaint, width, 
                    Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);  

        // here you can draw a rectangle — border for your popup text. 
        // You can use sl.getWidth() and sl.getWidth() methods to get the popup dimensions  

        canvas.save();
        canvas.translate(selectedPoint.x, selectedPoint.y);
        sl.draw(canvas);
        canvas.restore()
    }
}

At first, your need one global Point with your X,Y coordinates

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
    int X = (int)ev.getX();          
    int Y = (int)ev.getY();

    selectedPoint = new Point(X,Y);
    return true;
}

Now, you should override the onDraw() method to draw you popup view. There are many solutions to do that, I've tried this one with StaticLayout:

@Override
public void draw (Canvas canvas, MapView mapView, boolean shadow){
    super.draw(canvas, mapView, false); 
    if ( selectedPoint != null) {

        // here you should define PopupText, textPaint and width variables

        StaticLayout sl = new StaticLayout(popupText, textPaint, width, 
                    Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);  

        // here you can draw a rectangle — border for your popup text. 
        // You can use sl.getWidth() and sl.getWidth() methods to get the popup dimensions  

        canvas.save();
        canvas.translate(selectedPoint.x, selectedPoint.y);
        sl.draw(canvas);
        canvas.restore()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文