如何从infowindow内部调用非活动类的活动

时间:2012-08-16 07:51:25

标签: android google-maps android-intent infowindow

我正在尝试从非活动类调用一个活动,但我无法完成此任务。我的目标是在信息窗口中单击图像时从信息窗口调用新活动。如何从非活动类调用活动?感谢任何帮助。谢谢。

package com.icons.draw.view;

 import java.util.Iterator; 
  import java.util.List;

 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Point;
 import android.graphics.RectF;
 import android.graphics.Paint.Style;
 import android.os.Handler;
 import android.util.Log;
 import android.widget.Toast;

 import com.google.android.maps.GeoPoint;
 import com.google.android.maps.MapView;
 import com.google.android.maps.Overlay;

 import com.icons.draw.R;





 public class MapLocationOverlay  extends Overlay {

/**
 * Stored as global instances as one time initialization is enough
 */
private Bitmap mBubbleIcon, mShadowIcon;

private LocationViewers mLocationViewers;

private Paint   mInnerPaint, mBorderPaint, mTextPaint;

private Bitmap iconForMapKit,iconForMapKitRollOver;

private Handler mHandler=new Handler();

private boolean flag=false;

 private int [] start,end ; 

 private boolean checkAnimationEnded; 


private Point arrowPointCoordinates = new Point(); 

/**
 * It is used to track the visibility of information window and clicked location is known location or not 
 * of the currently selected Map Location
 */
private MapLocation mSelectedMapLocation;  
private void fillYCoordinateArrayForPinDropAnimation(LocationViewers  mapLocationViewer) 
{  
    List<MapLocation> mList = mapLocationViewer.getMapLocations(); 
    int size = mList.size(); 
    start = new int[size]; 
    end = new int[size]; 
} 
private boolean checkTwoArrayForEquality(int [] a , int [] b) 
{ 
  boolean result = true ; 

  for(int i = 0 ; i< a.length ; i++) 
  { 
      if(a[i] < b[i]){ result = false; break; } 
  }  
  Log.v("Coor", "Coor Resut = "+ result); 
  return result; 
} 



public MapLocationOverlay(LocationViewers mLocationViewers) {


    this.mLocationViewers = mLocationViewers;

    mBubbleIcon = BitmapFactory.decodeResource(mLocationViewers.getResources(),R.drawable.bubble);
    mShadowIcon = BitmapFactory.decodeResource(mLocationViewers.getResources(),R.drawable.shadow);
    iconForMapKit = BitmapFactory.decodeResource(mLocationViewers.getResources(),R.drawable.arrowformapkit); 
    iconForMapKitRollOver = BitmapFactory.decodeResource(mLocationViewers.getResources(),R.drawable.arrowformapkit_rollover); 
    fillYCoordinateArrayForPinDropAnimation(mLocationViewers); 

}



@Override
public boolean onTap(GeoPoint p, final MapView mapView)  {

    /**
     * Track the popup display
     */
    boolean isRemovePriorPopup = mSelectedMapLocation != null;  

    /**
     * Test whether a new popup should display
     */
      if(moreArrowTappedEvent(mapView,p) && isRemovePriorPopup) 
        { 
          // Toast.makeText(this.mLocationViewers.getContext(), "I am hit", Toast.LENGTH_LONG).show(); 

                         /*  Intent intent=new Intent();
          intent.setClass(this.mLocationViewers.getContext(), NewActivity.class); 
          startActivity(intent);*/





            flag = true; 
            mapView.invalidate(); 

            mHandler.postDelayed(new Runnable() { 

                public void run() { 
                    // TODO Auto-generated method stub 
                    flag = false; 
                    mapView.invalidate(); 
                } 
            },200L); 



        }
            else{
    mSelectedMapLocation = getHitMapLocation(mapView,p);
    if ( isRemovePriorPopup || mSelectedMapLocation != null) {
        mapView.invalidate();
    }   }   

    /**
     *   Return true if we handled this onTap()
     */
    return mSelectedMapLocation != null;
}

private boolean moreArrowTappedEvent(MapView mapView, GeoPoint tapPoint) {
     boolean result = false; 

        RectF hitTestRecr = new RectF(); 
        Point screenCoords = new Point(); 
        // Create a 'hit' testing Rectangle w/size and coordinates of our icon 
        // Set the 'hit' testing Rectangle with the size and coordinates of our on screen icon 
        hitTestRecr.set(arrowPointCoordinates.x,arrowPointCoordinates.y,arrowPointCoordinates.x+iconForMapKit.getWidth(),arrowPointCoordinates.y+iconForMapKit.getHeight()); 


        //  Finally test for a match between our 'hit' Rectangle and the location clicked by the user 
        mapView.getProjection().toPixels(tapPoint, screenCoords); 
        if (hitTestRecr.contains(screenCoords.x,screenCoords.y)) { 
            result = true; 
        } 
        return result;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

    drawMapLocations(canvas, mapView, shadow);
    drawInfoWindow(canvas, mapView, shadow);

 if(!checkTwoArrayForEquality(start, end)) 
 { 
     for(int i = 0; i<start.length ; i++) 
     { 
         if(start[i] < end[i] ) start[i]+=3; 
     } 
     mapView.invalidate(); 
 } 
 else 
 { 

     checkAnimationEnded = true; 
 }    


}

/**
 * Test whether an information balloon should be displayed or a prior balloon hidden.
 */
private MapLocation getHitMapLocation(MapView   mapView, GeoPoint   tapPoint) {

      MapLocation hitMapLocation = null; 

        RectF hitTestRecr = new RectF(); 
        Point screenCoords = new Point(); 
        Iterator<MapLocation> iterator = mLocationViewers.getMapLocations().iterator(); 
        while(iterator.hasNext()) { 
            MapLocation testLocation = iterator.next(); 

            //  Translate the MapLocation's lat/long coordinates to screen coordinates 
            mapView.getProjection().toPixels(testLocation.getPoint(), screenCoords); 

            // Create a 'hit' testing Rectangle w/size and coordinates of our icon 
            // Set the 'hit' testing Rectangle with the size and coordinates of our on screen icon 
            hitTestRecr.set(-mBubbleIcon.getWidth()/2,-mBubbleIcon.getHeight(),mBubbleIcon.getWidth()/2,0); 
            hitTestRecr.offset(screenCoords.x,screenCoords.y); 

            //  Finally test for a match between our 'hit' Rectangle and the location clicked by the user 
            mapView.getProjection().toPixels(tapPoint, screenCoords); 
            if (hitTestRecr.contains(screenCoords.x,screenCoords.y)) { 
                hitMapLocation = testLocation; 
                break; 
            } 
        } 

        //  Lastly clear the newMouseSelection as it has now been processed 
        tapPoint = null; 

        return hitMapLocation;  

}

private void drawMapLocations(Canvas canvas, MapView    mapView, boolean shadow) {

     Iterator<MapLocation> iterator = mLocationViewers.getMapLocations().iterator(); 
        Point screenCoords = new Point(); 

        int pos = 0; // for drop pin effect  
        while(iterator.hasNext()) {     
            MapLocation location = iterator.next(); 
            mapView.getProjection().toPixels(location.getPoint(), screenCoords); 
            shadow = false ; // remove this line if want shadow to be drawn also..  

            end[pos] = screenCoords.y - mBubbleIcon.getHeight();// for drop pin effect 
            if (shadow) { 
                //  Only offset the shadow in the y-axis as the shadow is angled so the base is at x=0;  
                canvas.drawBitmap(mShadowIcon, screenCoords.x, screenCoords.y - mShadowIcon.getHeight(),null); 
            }  
            else { 
                if(checkAnimationEnded) 
                { 
                    canvas.drawBitmap(mBubbleIcon, screenCoords.x - mBubbleIcon.getWidth()/2, screenCoords.y - mBubbleIcon.getHeight(),null); 
                } 
                else 
                { 
                    canvas.drawBitmap(mBubbleIcon, screenCoords.x - mBubbleIcon.getWidth()/2, start[pos],null); // for drop pin effect 
                }    


                //canvas.drawBitmap(bubbleIcon, screenCoords.x - bubbleIcon.getWidth()/2, screenCoords.y - bubbleIcon.getHeight(),null); 
            } 

            pos++;// for drop pin effect 
        } 

}

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

    if ( mSelectedMapLocation != null) { 
        if ( shadow) { 
            //  Skip painting a shadow in this tutorial 
        } else { 
            //  First determine the screen coordinates of the selected MapLocation 
            Point selDestinationOffset = new Point(); 
            mapView.getProjection().toPixels(mSelectedMapLocation.getPoint(), selDestinationOffset); 

            //  Setup the info window with the right size & location 
            int INFO_WINDOW_WIDTH = 200; 
            int INFO_WINDOW_HEIGHT = 50; 
            RectF infoWindowRect = new RectF(0,0,INFO_WINDOW_WIDTH,INFO_WINDOW_HEIGHT);              
            int infoWindowOffsetX = selDestinationOffset.x-INFO_WINDOW_WIDTH/2; 
            int infoWindowOffsetY = selDestinationOffset.y-INFO_WINDOW_HEIGHT-mBubbleIcon.getHeight(); 
            infoWindowRect.offset(infoWindowOffsetX,infoWindowOffsetY); 

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

            //  Draw border for info window 
            canvas.drawRoundRect(infoWindowRect, 5, 5, getmBorderPaint()); 

            //  Draw the MapLocation's name 
            int TEXT_OFFSET_X = 10; 
            int TEXT_OFFSET_Y = 15; 
            String name = mSelectedMapLocation.getName(); 
            if(name.length() >= 28) 
            { 
                name = name.substring(0, 26)+".."; 
            }    
            canvas.drawText(name,infoWindowOffsetX+TEXT_OFFSET_X,infoWindowOffsetY+TEXT_OFFSET_Y,getmTextPaint()); 
        //  canvas.drawText(selectedMapLocation.getPrice(),infoWindowOffsetX+TEXT_OFFSET_X,infoWindowOffsetY+TEXT_OFFSET_Y+20,getTextPaint()); 
            if(!flag) 
            { 
                canvas.drawBitmap(iconForMapKit, infoWindowOffsetX+160,infoWindowOffsetY+10, null);  
            } 
            else 
            { 
                canvas.drawBitmap(iconForMapKitRollOver, infoWindowOffsetX+160,infoWindowOffsetY+10, null); 
            }    

            arrowPointCoordinates.x = infoWindowOffsetX+160; 
            arrowPointCoordinates.y = infoWindowOffsetY+10; 
        } 
    } 
}

public Paint getmInnerPaint() {
    if ( mInnerPaint == null) {
        mInnerPaint = new Paint();
        mInnerPaint.setARGB(225, 50, 50, 50); //inner color
        mInnerPaint.setAntiAlias(true);
    }
    return mInnerPaint;
}

public Paint getmBorderPaint() {
    if ( mBorderPaint == null) {
        mBorderPaint = new Paint();
        mBorderPaint.setARGB(255, 255, 255, 255);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setStyle(Style.STROKE);
        mBorderPaint.setStrokeWidth(2);
    }
    return mBorderPaint;
}

public Paint getmTextPaint() {
    if ( mTextPaint == null) {
        mTextPaint = new Paint();
        mTextPaint.setARGB(255, 255, 255, 255);
        mTextPaint.setAntiAlias(true);
    }
    return mTextPaint;
}

}

5 个答案:

答案 0 :(得分:4)

我在您的商家信息中看到此代码已注释:

Intent intent=new Intent();
intent.setClass(this.mLocationViewers.getContext(), NewActivity.class); 
startActivity(intent);

为什么评论?基本上,这是您调用新活动的方式。

修改
我知道了。 startActivity()应该由context对象调用,否则它将说明未定义的方法。

在您的班级MapLocationOverlay中创建一个Context类型的新成员变量,然后修改您的构造函数以接受Context参数:

private Context mContext;

public MapLocationOverlay(Context context, LocationViewers mLocationViewers){
    this.mContext = context;
   //..........
}

然后你可以这样叫startActivity()

mContext.startActivity(intent);

显然,当你实例化MapLocationOverlay时,你也需要传递一个上下文引用。 例如:

 = new MapLocationOverlay(this, mLocationViewers);

答案 1 :(得分:0)

您需要为您的活动覆盖,以便它具有UI上下文实例。 比起startACtivity(),可能使用FLAG_NEW_TASK

答案 2 :(得分:0)

    (activity) myContext.startActivity(myintent)

将您的上下文转换为活动,然后尝试。

答案 3 :(得分:0)

试试这些代码。 在MainActivity中创建方法。

public static goToAnotherActivity(){
    Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(MainActivity.getContext(), AnotherActivity.class);
MainActivity.getContext().startActivity(i);
}

并从你的非活动类中调用它。

MainActivity.goToAnotherActivity();

希望这对你有所帮助。

答案 4 :(得分:0)

试试这个: context.startActivity(intent);