1

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

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

5 回答 5

4

我在您的清单中看到了此代码,并对此进行了评论:

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

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

编辑:
我明白了。startActivity()应该由一个对象调用,context否则它会说它是未定义的方法。

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

private Context mContext;

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

然后你会这样称呼startActivity()

mContext.startActivity(intent);

显然,当您实例化 MapLocationOverlay 时,您也需要将上下文引用传递给它。前任:

 = new MapLocationOverlay(this, mLocationViewers);
于 2012-08-16T07:55:10.527 回答
0

您需要为该 Overlay 提供您的 Activity 以便它具有 UI Context 实例。比 startACtivity() ,可能与 FLAG_NEW_TASK

于 2012-08-16T07:55:47.013 回答
0
    (activity) myContext.startActivity(myintent)

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

于 2012-08-16T08:45:03.600 回答
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();

希望这对您有所帮助。

于 2013-05-15T02:29:56.283 回答
0

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

于 2013-08-16T02:21:41.450 回答