如何从infowindow内部的非活动类中调用活动
问题描述:
我想从非活动类调用活动,但我无法完成此任务。我的目标是从信息窗口调用新活动,何时信息窗口中的图像被点击。我如何从非活动课程调用活动?任何帮助表示感谢。谢谢。如何从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;
}
}
答
我看到你的房源这个代码被注释:
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);
答
您需要给出该覆盖您的活动,以便它有一个UI上下文实例。 比startACtivity()上,可能与FLAG_NEW_TASK
答
(activity) myContext.startActivity(myintent)
投你的背景下进入一个活动,然后再尝试。
答
试试这些代码。 在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();
希望这对你有所帮助。
答
试试这个:
我试图使用,但它不工作,它不能解决startActivity(意向)。它说,创建一个startActivity方法 – asdfg 2012-08-16 08:04:33
startActivity只能从上下文调用,我假设你把它叫做这个!= context – MikeIsrael 2012-08-16 08:08:36
@sesamos,我编辑了我的答案。重读它。 – 2012-08-16 08:15:05