MapView 叠加弹出窗口与布局失去焦点

发布于 2024-09-16 15:31:44 字数 4495 浏览 2 评论 0原文

我正在尝试实现一个像谷歌地图网络版一样的信息窗口。 我创建了一个扩展线性布局的自定义布局,并用 TextView 和按钮填充它。

我已经重写了balloonLayout(它扩展了LinearLayout)的dispatchDraw方法来绘制带有圆角和气球尖端的容器。

在我的自定义覆盖类上,我将点击坐标保存到变量中,并在之前保存的坐标处的重写绘制方法上绘制信息窗口。信息窗口绘制正常,但按钮失去焦点并且永远不会调用它的 onClick 侦听器,它可以被单击(它没有获得按下的黄色状态),我不明白为什么......

提前谢谢您。

这是我的 ballonlayout.class 调度绘制方法:

@Override
protected void dispatchDraw(Canvas canvas)
{
    Paint panelPaint  = new Paint();
    panelPaint.setARGB(0, 0, 0, 0);

    RectF panelRect = new RectF();
    panelRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
    canvas.drawRoundRect(panelRect, 5, 5, panelPaint);

    RectF baloonRect = new RectF();
    baloonRect.set(0,0, getMeasuredWidth(), 2*(getMeasuredHeight()/3));
    panelPaint.setARGB(230, 255, 255, 255);        
    canvas.drawRoundRect(baloonRect, 10, 10, panelPaint);

    Path baloonTip = new Path();
    baloonTip.moveTo(5*(getMeasuredWidth()/8), 2*(getMeasuredHeight()/3));
    baloonTip.lineTo(getMeasuredWidth()/2, getMeasuredHeight());
    baloonTip.lineTo(3*(getMeasuredWidth()/4), 2*(getMeasuredHeight()/3));

    canvas.drawPath(baloonTip, panelPaint);

    super.dispatchDraw(canvas);
}   

这是 ballonlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.preciotaxi.balloonLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/transparent_panel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5px"
    android:paddingTop="5px"
    android:paddingRight="5px"
    android:paddingBottom="5px"
    android:orientation="vertical"
    >

  <TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonLabel"
  android:text="coordenadas"
  />  

  <Button 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonButton"
  android:text="Ir aquí"
  android:clickable="true"

  />
  </com.forgottenprojects.preciotaxi.balloonLayout>

和 tapOverlay.class

class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
private Context context;
LayoutInflater inflater;
balloonLayout noteBaloon;
public tapOverlay(Context c)
{
    this.context=c;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    noteBaloon = (balloonLayout) inflater.inflate(R.layout.balloonlayout, null);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200,100); 
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    noteBaloon.setLayoutParams(layoutParams); 

}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
    lastTap = p;
    mapView.getController().animateTo(p);
    return true;        
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{
    Projection p = mapView.getProjection();
    Paint paint = new Paint();
    paint.setColor(0xFFFFFF00);
    paint.setStrokeWidth(5);
    paint.setAntiAlias(true);
    if(lastTap!=null)
    {           
        drawInfoWindow(canvas, p, shadow, mapView);
    }
   super.draw(canvas, mapView, shadow);
}

private void drawInfoWindow(Canvas canvas, Projection myprojection,
        boolean shadow, MapView mapView) 
{
    try{
        mapView.removeView(noteBaloon);
        noteBaloon.setVisibility(View.VISIBLE);

        TextView textmsg = (TextView) noteBaloon.findViewById(R.id.ballonLabel);
        textmsg.setText(lastTap.getLatitudeE6()+","+lastTap.getLongitudeE6());
        Button btn = (Button) noteBaloon.findViewById(R.id.ballonButton);
        btn.setFocusable(true);
        btn.setOnClickListener(btn_onclick);
        mapView.addView(noteBaloon, new MapView.LayoutParams(200,200,lastTap,MapView.LayoutParams.BOTTOM_CENTER));
        mapView.setEnabled(true);

        mapView.invalidate();
    } catch (Exception e)
    {
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private OnClickListener btn_onclick = new OnClickListener() {
    //This never shows...
    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Se hizo clic... y ahora?", Toast.LENGTH_LONG).show();
    }
};

}

I'm trying to implement an infowindow like the web version of google maps.
I have created a custom layout extending a linear layout and populated it with a TextView and a button.

I have overrided the dispatchDraw method of the balloonLayout (wich extends LinearLayout) to draw a container with rounded corners and the balloon tip.

On my custom overlay class I save the tap coordinates to a variable and I draw the infowindow on the overrided draw method at the coordinates previously saved. The Info window draws OK but the button loses focus and never call it's onClick listener, It can be clicked (it doesn't get the pressed yellow state) and I can't figure why...

Thank you in advance.

This is my balloonlayout.class dispatchdraw method:

@Override
protected void dispatchDraw(Canvas canvas)
{
    Paint panelPaint  = new Paint();
    panelPaint.setARGB(0, 0, 0, 0);

    RectF panelRect = new RectF();
    panelRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
    canvas.drawRoundRect(panelRect, 5, 5, panelPaint);

    RectF baloonRect = new RectF();
    baloonRect.set(0,0, getMeasuredWidth(), 2*(getMeasuredHeight()/3));
    panelPaint.setARGB(230, 255, 255, 255);        
    canvas.drawRoundRect(baloonRect, 10, 10, panelPaint);

    Path baloonTip = new Path();
    baloonTip.moveTo(5*(getMeasuredWidth()/8), 2*(getMeasuredHeight()/3));
    baloonTip.lineTo(getMeasuredWidth()/2, getMeasuredHeight());
    baloonTip.lineTo(3*(getMeasuredWidth()/4), 2*(getMeasuredHeight()/3));

    canvas.drawPath(baloonTip, panelPaint);

    super.dispatchDraw(canvas);
}   

This is the ballonlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.preciotaxi.balloonLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/transparent_panel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5px"
    android:paddingTop="5px"
    android:paddingRight="5px"
    android:paddingBottom="5px"
    android:orientation="vertical"
    >

  <TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonLabel"
  android:text="coordenadas"
  />  

  <Button 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonButton"
  android:text="Ir aquí"
  android:clickable="true"

  />
  </com.forgottenprojects.preciotaxi.balloonLayout>

And the tapOverlay.class

class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
private Context context;
LayoutInflater inflater;
balloonLayout noteBaloon;
public tapOverlay(Context c)
{
    this.context=c;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    noteBaloon = (balloonLayout) inflater.inflate(R.layout.balloonlayout, null);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200,100); 
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    noteBaloon.setLayoutParams(layoutParams); 

}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
    lastTap = p;
    mapView.getController().animateTo(p);
    return true;        
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{
    Projection p = mapView.getProjection();
    Paint paint = new Paint();
    paint.setColor(0xFFFFFF00);
    paint.setStrokeWidth(5);
    paint.setAntiAlias(true);
    if(lastTap!=null)
    {           
        drawInfoWindow(canvas, p, shadow, mapView);
    }
   super.draw(canvas, mapView, shadow);
}

private void drawInfoWindow(Canvas canvas, Projection myprojection,
        boolean shadow, MapView mapView) 
{
    try{
        mapView.removeView(noteBaloon);
        noteBaloon.setVisibility(View.VISIBLE);

        TextView textmsg = (TextView) noteBaloon.findViewById(R.id.ballonLabel);
        textmsg.setText(lastTap.getLatitudeE6()+","+lastTap.getLongitudeE6());
        Button btn = (Button) noteBaloon.findViewById(R.id.ballonButton);
        btn.setFocusable(true);
        btn.setOnClickListener(btn_onclick);
        mapView.addView(noteBaloon, new MapView.LayoutParams(200,200,lastTap,MapView.LayoutParams.BOTTOM_CENTER));
        mapView.setEnabled(true);

        mapView.invalidate();
    } catch (Exception e)
    {
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private OnClickListener btn_onclick = new OnClickListener() {
    //This never shows...
    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Se hizo clic... y ahora?", Toast.LENGTH_LONG).show();
    }
};

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文