English 中文(简体)
Osmdroid. on TroitsSingleTapUp 不为自定义可抓取工作
原标题:Osmdroid. onItemSingleTapUp not works for custom Drawable

I have created custom drawable marker which uses canvas to draw bounds. Everything works great excepts one thing: onItemSingleTapUp not called when any marker on the screen taped. Here is overlay creation code:

ItemizedIconOverlay<OverlayItem> groupsOverlay = new ItemizedIconOverlay<OverlayItem>(
    new ArrayList<OverlayItem>(),
    new OnItemGestureListener<OverlayItem>() {

        @Override
        public boolean onItemLongPress(int arg0, OverlayItem arg1) {                                
            return false;
        }

        @Override
        public boolean onItemSingleTapUp(int arg0, OverlayItem arg1) {

            if(arg1 == null){
                return false;
            }

            if(m_prevView != null){
                m_mapView.removeView(m_prevView);
                m_prevView = null;
            }

            View popUp = getLayoutInflater().inflate(R.layout.map_popup, m_mapView, false);
            TextView tv = (TextView)popUp.findViewById(R.id.popupTextView);
            tv.setText(arg1.getTitle());

            MapView.LayoutParams mapParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT,
        arg1.getPoint(),
        MapView.LayoutParams.BOTTOM_CENTER, 0, 0);
            m_mapView.addView(popUp, mapParams);
            m_prevView = popUp;

            return true;
        }
    }, new DefaultResourceProxyImpl(getApplicationContext()));

这是自定义的绘图标记 :

package com.testapp.data; 
import android.graphics.Canvas; 
import android.graphics.ColorFilter; 
import android.graphics.Paint; 
import android.graphics.Paint.Align; 
import android.graphics.Paint.Style; 
import android.graphics.PixelFormat; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.graphics.drawable.Drawable; 
public class GroupMarkerDrawable extends Drawable { 
        private final static int DELTA_BOX = 4; 
        private final Paint m_paint; 
        private String m_text; 
        private double m_pxRadius; 
        public GroupMarkerDrawable(double pxRadius, String text) { 
                m_text = text; 
                m_paint = new Paint(); 
                m_pxRadius = pxRadius; 
                m_paint.setAntiAlias(true); 
        } 
        @Override 
    public void draw(Canvas c) { 
                // Set the correct values in the Paint 
        m_paint.setARGB(190, 0, 0, 0); 
        m_paint.setStrokeWidth(2); 
        m_paint.setStyle(Style.STROKE); 
        m_paint.setTextAlign(Align.CENTER); 
        Rect bounds = new Rect(); 
        m_paint.getTextBounds(m_text, 0, m_text.length(), bounds); 
        int centerX = getBounds().centerX(); 
        int centerY = getBounds().centerY(); 
        int w2 = bounds.width() / 2; 
        int h2 = bounds.height() / 2; 
        Rect rect = new Rect(centerX - w2 - DELTA_BOX, centerY - h2 - 
DELTA_BOX, centerX + w2 + DELTA_BOX, centerY + h2 + DELTA_BOX); 
        // Draw it 
        c.drawCircle(centerX, centerY, (float) m_pxRadius, m_paint); 
        m_paint.setStyle(Style.FILL); 
        m_paint.setARGB(190, 0, 128, 0); 
        c.drawRect(rect, m_paint); 
        m_paint.setStyle(Style.STROKE); 
        m_paint.setARGB(190, 0, 0, 0); 
        c.drawRect(rect, m_paint); 
        c.drawText(m_text, centerX, centerY + h2, m_paint); 
    } 
        @Override 
        public int getOpacity() { 
                return PixelFormat.OPAQUE; 
        } 
        @Override 
        public void setAlpha(int arg0) { 
        } 
        @Override 
        public void setColorFilter(ColorFilter arg0) { 
        } 
} 

Same code using static drawable from resources, instead of GroupMarkerDrawable, works.

最佳回答

我找到了如何解决这个问题。 只要需要将适当的维度返回到 Drawable 。 这需要以 Group MarkerDrowable 中两种方法压倒一切。 FOR 示例如下:

@Override
public int getIntrinsicHeight() {
    return m_pxRadius * 2;
};

@Override
public int getIntrinsicWidth() {
    return m_pxRadius * 2;
};
问题回答

An additional solution for a polygon. The solutions of ydanila put me on the good track but I had to override hitTest method of ItemizedOverlayWithFocus class in order to get my polygon hit.

可抓取 :

Drawable drawable = new Drawable() {

  private int mIntrinsicHeight = 0;
  private int mIntrinsicWidth = 0;

  @Override
  public void draw(Canvas canvas) {
    // used to determine limit coordinates of the drawable
    int yTop, yBottom, xLeft, xRight;

    if (points != null && points.size() > 1) {
      //we have to make a projection to convert from postions on the map in 
      //gradiant to a position on the view in pixels
      final Projection pj = mapView.getProjection();
      Path path = new Path();
      Point centerMapPixelPoint = new Point();
      Point tmpMapPixelPoint = new Point();

      pj.toMapPixels(points.get(0), centerMapPixelPoint);
      // init limit coordinates
      xLeft = centerMapPixelPoint.x;
      xRight = centerMapPixelPoint.x;
      yTop = centerMapPixelPoint.y;
      yBottom = centerMapPixelPoint.y;
      path.moveTo(centerMapPixelPoint.x, centerMapPixelPoint.y);
      for (int i = 1; i < points.size(); i++) {
        pj.toMapPixels(points.get(i), tmpMapPixelPoint);
        // update limit coordinates if necessary
        if (xLeft > tmpMapPixelPoint.x) {
          xLeft = tmpMapPixelPoint.x;
        }
        if (xRight < tmpMapPixelPoint.x) {
          xRight = tmpMapPixelPoint.x;
        }
        if (yBottom < tmpMapPixelPoint.y) {
          yBottom = tmpMapPixelPoint.y;
        }
        if (yTop > tmpMapPixelPoint.y) {
          yTop = tmpMapPixelPoint.y;
        }
        path.lineTo(tmpMapPixelPoint.x, tmpMapPixelPoint.y);
      }
      // close polygon returning to first point
      path.close();

      canvas.drawPath(path, linePaint);
      canvas.drawPath(path, innerPaint);
      // calculate drawable height and width
      mIntrinsicHeight = yTop -yBottom;
      mIntrinsicWidth = xRight - xLeft;
    }
  }

  @Override
  public int getIntrinsicHeight() {
    return mIntrinsicHeight;
  };

  @Override
  public int getIntrinsicWidth() {
    return mIntrinsicWidth;
  };

};

重叠 :

public class MyItemizedIconOverlay<Item extends OverlayItem> extends ItemizedOverlayWithFocus<Item> {

  @Override
  protected boolean hitTest(Item item, Drawable marker, int hitX, int hitY) {
    boolean hit = false;

    Rect bounds = marker.getBounds();
    if (hitX < bounds.right && hitX > bounds.left && hitY < bounds.top && hitY > bounds.bottom) {
      hit = true;
    } else {
      hit = false;
    }


    return hit;
  };




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签