我开发了一个地图应用程序,我想在地图屏幕的右上方显示指南针。指南针也准备工作和地图但是当我把它们放在一起时它不显示指南针(我的意思是当我在自定义mapview类的调度绘制中绘制指南针时它的提升问题)
奇怪的是我制作了两个画布对象 1.绘图路径指南针(箭头) 2.文字“N” 这里显示它的显示文字然而它不是显示罗盘。
这里是Custom Mapview类的清晰度..
package com.Espiritos.Home.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.core.GeoPoint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import com.Espiritos.Utility.AppLog;
/**-----------------------------------------------------------------------------
Class Name: MyMapView
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: All Listener and Canvas method are introduce in this class for map operation
-----------------------------------------------------------------------------*/
public class MyMapView extends MapView
{
private float mfDirection = 0;
private float[] mfRotateValue;
private Paint mPntCompass = new Paint(Paint.ANTI_ALIAS_FLAG);
private boolean mIsFirstDraw;
/** ------------------------------------------------------------------------
// LISTENER DEFINITIONS
// ------------------------------------------------------------------------
*/
/** Change listener for mapview*/
public interface OnChangeListener
{
public void onChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter, int newZoom, int oldZoom);
}
/** ------------------------------------------------------------------------
// MEMBERS
// ------------------------------------------------------------------------
*/
private MyMapView mThis;
@Override
protected void onDraw(Canvas amcanvas) {
super.onDraw(amcanvas);
}
private long mlEventsTimeout = 1500;//250L; // Set this variable to your preferred timeout
private boolean mIsTouched = false;
private GeoPoint mLastCenterPosition;
private int miLastZoomLevel;
private Timer mChangeDelayTimer = new Timer();
private MyMapView.OnChangeListener mChangeListener = null;
private int miOldZoomLevel = -1;
private OnClusterMapZoomListener onClusterMapZoomListener;
/**-----------------------------------------------------------------------------
Method Name: MyMapView(CONSTRUCTORS)
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: Get Context from base and use in operation
-----------------------------------------------------------------------------*/
public MyMapView(Context amcontx)
{
super(amcontx);
init();
}
public MyMapView(Context amcontext, AttributeSet amAttrs)
{
super(amcontext, amAttrs);
init();
}
public MyMapView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
init();
}
/**-----------------------------------------------------------------------------
Method Name: init
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: get Mapcenter and Map ZoomLevel
-----------------------------------------------------------------------------*/
private void init()
{
mThis = this;
mLastCenterPosition = this.getMapPosition().getMapCenter();
miLastZoomLevel = this.getMapPosition().getZoomLevel();
mPntCompass.setStyle(Paint.Style.FILL_AND_STROKE);
mPntCompass.setStrokeWidth(4);
mPntCompass.setColor(Color.GRAY);
mPntCompass.setTextSize(30);
mPntCompass.setAntiAlias(true);
mPntCompass.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.REPEAT));
mIsFirstDraw = true;
}
/**-----------------------------------------------------------------------------
Method Name: setOnChangeListener
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: Mapview change listener implementation
-----------------------------------------------------------------------------*/
public void setOnChangeListener(MyMapView.OnChangeListener l)
{
mChangeListener = l;
}
/**-----------------------------------------------------------------------------
Method Name: onTouchEvent
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: on touch event handling with motionEvent
-----------------------------------------------------------------------------*/
@Override
public boolean onTouchEvent(MotionEvent ev)
{
/** Set touch internal*/
mIsTouched = (ev.getAction() != MotionEvent.ACTION_UP);
return super.onTouchEvent(ev);
}
/**-----------------------------------------------------------------------------
Method Name: computeScroll
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: scroll change counting by this method
-----------------------------------------------------------------------------*/
@Override
public void computeScroll()
{
super.computeScroll();
/**Check for change*/
if (isSpanChange() || isZoomChange())
{
/**
* If computeScroll called before timer counts down
we should drop it and
start counter over again
*/
resetMapChangeTimer();
}
}
/**-----------------------------------------------------------------------------
Method Name: resetMapChangeTimer
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: after scroll it get change the position, it getting new position update
-----------------------------------------------------------------------------*/
private void resetMapChangeTimer()
{
mChangeDelayTimer.cancel();
mChangeDelayTimer = new Timer();
mChangeDelayTimer.schedule(new TimerTask()
{
@Override
public void run()
{
if (mChangeListener != null) mChangeListener.onChange(mThis, getMapPosition().getMapCenter(), mLastCenterPosition, getMapPosition().getZoomLevel(), miLastZoomLevel);
mLastCenterPosition = getMapPosition().getMapCenter();
miLastZoomLevel = getMapPosition().getZoomLevel();
}
}, mlEventsTimeout);
}
/**-----------------------------------------------------------------------------
Method Name: isSpanChange
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: after scroll it get change the position, it getting new position update
-----------------------------------------------------------------------------*/
private boolean isSpanChange()
{
return !mIsTouched && !getMapPosition().getMapCenter().equals(mLastCenterPosition);
}
/**-----------------------------------------------------------------------------
Method Name: isZoomChange
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: Get current zoom level
-----------------------------------------------------------------------------*/
private boolean isZoomChange()
{
return (getMapPosition().getZoomLevel() != miLastZoomLevel);
}
/**-----------------------------------------------------------------------------
Method Name: dispatchDraw
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: This method is used to Draw Polygon in map
-----------------------------------------------------------------------------*/
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// Notify listener, if not null, that the zoom level changed
if (getMapPosition().getZoomLevel() != miOldZoomLevel) {
miOldZoomLevel = getMapPosition().getZoomLevel();
if (onClusterMapZoomListener != null) {
onClusterMapZoomListener.onZoomLevelChanged(miOldZoomLevel);
}
}
if (!mIsFirstDraw)
{
Path mPathArrow = new Path();
mPathArrow.moveTo(0, -50);
mPathArrow.lineTo(-20, 40);
mPathArrow.lineTo(0, 30);
mPathArrow.lineTo(20, 40);
mPathArrow.close();
Paint mPntText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPntText.setAntiAlias(true);
mPntText.setTextSize(30);
mPntText.setColor(Color.BLACK);
mPntText.setStyle(Paint.Style.FILL);
int miCanvasWidth = canvas.getWidth();
int miCanvasHeight = canvas.getHeight();
int miCenterX = miCanvasWidth / 2;
int miCenterY = miCanvasHeight / 2;
canvas.translate(miCanvasWidth-miCanvasWidth/10, miCanvasHeight/10);
if (mfRotateValue != null) {
canvas.rotate(-mfRotateValue[0]);
}
canvas.drawPath(mPathArrow, mPntCompass);
canvas.drawText("N", miCanvasWidth-miCanvasWidth/10, miCanvasHeight/10, mPntText);
}
}
public void updateDirection(float dir,float[] mevent)
{
mIsFirstDraw = false;
mfDirection = dir;
this.mfRotateValue = mevent;
Log.e(VIEW_LOG_TAG, "direction"+mfDirection);
invalidate();
}
/**-----------------------------------------------------------------------------
Method Name: getOnClusterMapZoomListener
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: get Map Zoomlistner implemented
-----------------------------------------------------------------------------*/
public OnClusterMapZoomListener getOnClusterMapZoomListener()
{
return onClusterMapZoomListener;
}
/**-----------------------------------------------------------------------------
Method Name: setOnClusterMapZoomListener
Created By: 704
Created Date:
Modified By:
Modified Date:
Purpose: set Map Zoomlistner implemented
-----------------------------------------------------------------------------*/
public void setOnClusterMapZoomListener(
OnClusterMapZoomListener onClusterMapZoomListener)
{
this.onClusterMapZoomListener = onClusterMapZoomListener;
}
}
注意:我为此制作了演示应用程序,并且显示效果非常好.... 2:我在samsun 10英寸平板电脑上运行的实时应用程序显示但在三星s3中它不是...
答案 0 :(得分:0)
我已经通过将Manifest文件minSdkversion = 3更改为8来解决问题,并解决了它的工作,令人惊讶的是它的工作,仍然不知道背后的逻辑......如果你知道那么让我知道它为什么会发生