在调试时旋转Mapview错误

时间:2012-02-23 12:41:43

标签: android map orientation android-mapview rotation

我开发了一个可以旋转方向视图的地图,取决于用户面部的位置(例如,如果用户转向南方,则地图将向南方向旋转,如指南针)。我已经尝试了这个项目,但在几周内没有成功。我在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;
        }

1 个答案:

答案 0 :(得分:0)

解决了这个问题,只需将i<=count更改为i<count,一切正常!但我的变焦和指南针没有显示出来。