2012-08-16 73 views
1

我想從非活動類調用活動,但我無法完成此任務。我的目標是從信息窗口調用新活動,何時信息窗口中的圖像被點擊。我如何從非活動課程調用活動?任何幫助表示感謝。謝謝。如何從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; 
} 

}

回答

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時,你也需要傳遞一個上下文引用。 ex:

= new MapLocationOverlay(this, mLocationViewers); 
+0

我試圖使用,但它不工作,它不能解決startActivity(意向)。它說,創建一個startActivity方法 – asdfg 2012-08-16 08:04:33

+0

startActivity只能從上下文調用,我假設你把它叫做這個!= context – MikeIsrael 2012-08-16 08:08:36

+0

@sesamos,我編輯了我的答案。重讀它。 – 2012-08-16 08:15:05

0

您需要給出該覆蓋您的活動,以便它有一個UI上下文實例。 比startACtivity()上,可能與FLAG_NEW_TASK

0
(activity) myContext.startActivity(myintent) 

投你的背景下進入一個活動,然後再嘗試。

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); 
} 

並從你的non_activity類調用它。

MainActivity.goToAnotherActivity(); 

希望這對你有所幫助。