2012-07-30 44 views
0

在我的Android地图应用程序中,我有一些集群图标。每当按下集群图标时,我想显示带有文本和按钮的弹出信息框。如果我们按下按钮,它将导致下一个活动。Android地图:在onDraw()中使用文本和按钮创建自定义视图

private void drawInfoWindow(Canvas canvas, MapView mapView, boolean shadow, 
      int x, int y) { 

     if (isSelected_) { 

       Point selDestinationOffset = new Point(); 
       mapView.getProjection().toPixels(cluster_.center_, 
         selDestinationOffset); 

       // Setup the info window with the right size & location 
       int INFO_WINDOW_WIDTH = 125; 
       int INFO_WINDOW_HEIGHT = 25; 
       RectF infoWindowRect = new RectF(0, 0, INFO_WINDOW_WIDTH, 
         INFO_WINDOW_HEIGHT); 


       infoWindowRect.offset(x, y); 

       // Draw inner info window 
       canvas.drawRoundRect(infoWindowRect, 5, 5, getInnerPaint()); 



       // Draw the MapLocation's name 
       int TEXT_OFFSET_X = 10; 
       int TEXT_OFFSET_Y = 15; 

       canvas.drawText(cluster_.number+" Incidents", x + TEXT_OFFSET_X, y 
         + TEXT_OFFSET_Y, getTextPaint()); 
      } 
     } 

这只显示一个像弹出窗口一样的小文本视图。但我想显示一个带有文本和按钮的弹出信息框。请给我提供最好的办法....

谢谢...

回答

1

您可以使用自定义布局的XML文件来创建的,而不是使用画布上MapView的一个覆盖。

  1. 创建您的覆盖XML文件(我用overlay_pin_layout.xml这里)。例如,顶部有文本视图和底部有按钮的线性布局。

  2. 将此叠加层添加到您想要的位置。

    //从充气XML文件

    查看overlayPin = getLayoutInflater()的覆盖膨胀(R.layout.overlay_pin_layout,NULL);

    //设置的TextView

    ((TextView的)(overlayPin.findViewById(R.id.textview_overlayerpin)))的setText(YOUR_TEXT)。

    //设置按钮触摸听者

    ((按钮)(overlayPin.findViewById(R.id.button_overlayerpin)))setOnLongClickListener。( 新View.OnLongClickListener(){

        @Override 
            public boolean onLongClick(View v) { 
             // Do something on button click 
             return false; 
            } 
           } 
          ); 
    

    //在MapView的通过GeoPoint对象添加覆盖

    overlayPin.setLayoutParams(新MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 新的GeoPoint( YOUR_LAT,YOUR_LON), MapView.LayoutParams.BOTTOM_CENTER)); mapView.addView(overlayPin);

对于像弹出式动画一样,试试这个。 在res中创建一个anim文件夹,如果没有的话。 使用下面的代码在anim文件夹中创建一个xml。我把它称为animation_pop_up.xml。

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<scale 
    android:fromXScale="0.0" 
    android:toXScale="1.0" 
    android:fromYScale="0.0" 
    android:toYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="500" 
    android:repeatCount="0" 
    android:repeatMode="reverse" 
    android:interpolator="@android:anim/accelerate_interpolator" 
    > 
</scale> 
<alpha 
    android:fromAlpha="0" 
    android:toAlpha="1" 
    android:duration="500" 
    android:repeatCount="0" 
    android:repeatMode="reverse"> 
</alpha> 
</set> 

当您在mapview上添加overlayPin时,播放动画。

Animation animation = AnimationUtils.loadAnimation(mainActivity.getApplicationContext(), 
        R.anim.animation_pop_up); 
overlayPin.startAnimation(animation); 
+0

它会显示为弹出窗口吗?我想在特定的GeoPoint上显示 – Sridhar 2012-07-30 08:10:25

+0

1.上面的代码只显示mapview上的文本按钮视图,没有弹出式动画。您需要在创建overlayPin时启动动画。 2.只需将YOUR_LAT和YOUR_LON更改为你想要的 – 2012-07-30 08:53:27

+0

当我们点击另一个图标时,它会显示另一个弹出窗口,最后一个显示的弹出窗口仍然存在。当点击新的弹出窗口时,如何关闭现有的弹出窗口 – Sridhar 2012-07-30 09:18:54