0

我开发了一个地图应用程序,我想在地图屏幕的右上角显示指南针。指南针也准备好工作和地图,但是当我将两者放在一起时,它不显示指南针(我的意思是当我在自定义 mapview 类的调度绘制中绘制指南针时它会引发问题)

奇怪的是,我制作了两个画布对象 1. 通过绘制路径(箭头) 2. 此处显示的文本“N”显示其显示文本,但它不显示罗盘。

这是为了清楚起见的自定义 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 中它不是...

4

1 回答 1

0

我已经通过将 Manifest 文件 minSdkversion=3 更改为 8 及其解决方案解决了这个问题,它的工作令人惊讶,但仍然不知道其背后的逻辑......如果你知道然后让我知道它为什么会发生

于 2013-01-24T07:49:44.797 回答