簡體   English   中英

調試時旋轉Mapview錯誤

[英]Rotate Mapview error on debug

我開發了一個可以旋轉方向視圖的地圖,具體取決於用戶所面對的位置(例如,如果用戶向南旋轉,則地圖將向南旋轉,例如指南針)。 我已經嘗試了兩個星期,但收效甚微。 我在Eclipse上沒有錯誤消息,但是當我調試代碼時,應用程序總是崩潰。 謝謝,我真的需要所有幫助

    private class RotateView extends ViewGroup  {

    private Matrix mMatrix = new Matrix();
    private float[] mTemp = new float [2];
    private static final float SQ2 = 1.414213562373095f;


    public RotateView(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }



    @Override
    protected void dispatchDraw(Canvas canvas) {
        // TODO Auto-generated method stub

        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(-mHeading,getWidth()*0.5f , getHeight()*0.5f);
        canvas.getMatrix(mMatrix);
        super.dispatchDraw(canvas);
        canvas.restore();
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        final int width = getWidth();
        final int height = getHeight();
        final int count = getChildCount();
        for (int i=0; i<=count; i++){
            final View view = getChildAt(i);
            final int childWidth = view.getMeasuredWidth();
            final int childHeight = view.getMeasuredHeight();
            final int childLeft = ((width - childWidth)/2);
            final int childTop = ((height-childHeight)/2);
            view.layout(childLeft, childTop, childLeft+childWidth, childTop+childHeight); 
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int mW = getDefaultSize(getMeasuredWidth(), widthMeasureSpec);
        int mH = getDefaultSize(getMeasuredHeight(), heightMeasureSpec);
        int sizeSpec;
        if(mW > mH){
            sizeSpec = MeasureSpec.makeMeasureSpec((int)(mW*SQ2), MeasureSpec.EXACTLY);
        } else {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (mH*SQ2), MeasureSpec.EXACTLY);
        }
        final int count = getChildCount();
        for(int i = 0; i<count; i++){
            getChildAt(i).measure(sizeSpec, sizeSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        // TODO Auto-generated method stub

        return super.dispatchTouchEvent(e);
    }

}
// End of class RotateView

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    sensormanager =(SensorManager)getSystemService(SENSOR_SERVICE);
    rotateview = new RotateView(this);
    map = new MapView (this,"Map Key");
    rotateview.addView(map);
    setContentView(rotateview);
    controll();
}

public void controll () {

        controller = map.getController();
        //Zoom
        map.setBuiltInZoomControls(true);
        controller.setZoom(19);
        overlayList = map.getOverlays();
        map.setClickable(true);
        map.setEnabled(true);
        //compass
        compass = new MyLocationOverlay(MapIpbActivity.this, map);
        overlayList.add(compass);
        myLocation();
        Touchy t = new Touchy();
        overlayList.add(t);
}

public void myLocation() {
    ourLocation = new MyLocationOverlay(MapIpbActivity.this, map);
    map.getOverlays().add(ourLocation);
    ourLocation.runOnFirstFix(new Runnable() {
        public void run(){
            controller.animateTo(ourLocation.getMyLocation());
        }

    });
    }



@Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();
    ourLocation.disableMyLocation();
    sensormanager.unregisterListener(orientListener);
    super.onPause();
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    compass.enableCompass();
    ourLocation.enableMyLocation();
    sensormanager.registerListener(orientListener, 
            sensormanager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            sensormanager.SENSOR_DELAY_NORMAL);

}

final SensorEventListener orientListener = new SensorEventListener() {

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
            mHeading = event.values[0];
            rotateview.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};
    class Touchy extends Overlay {
        long start;
        long stop;
        int x;
        int y;
        GeoPoint TouchedPoint;

        @Override
        public boolean onTouchEvent(MotionEvent e, MapView m) {
            // TODO Auto-generated method stub
            if(e.getAction() == MotionEvent.ACTION_DOWN) {
                start = e.getEventTime();
                x = (int) e.getX();
                y = (int) e.getY();
                TouchedPoint = map.getProjection().fromPixels(x, y);
            }
            if (e.getAction() == MotionEvent.ACTION_UP) {
                stop = e.getEventTime();
            }if (stop - start > 2000){
                    box();
                    return true;
            }
            return false;
        }

解決了這個問題,只需將i<=count更改為i<count ,一切都會正常! 但是我的變焦和指南針沒有顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM