⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapviewcompassdemo.java

📁 android 例子中的确良ApiDemos。很有代表意义
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.android.apis.view;import javax.microedition.khronos.opengles.GL;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.DrawFilter;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Picture;import android.graphics.PorterDuff;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Region;import android.hardware.SensorManager;import android.hardware.SensorListener;import com.google.android.maps.MapActivity;import com.google.android.maps.MapView;import com.google.android.maps.MyLocationOverlay;/** * Example of how to use an {@link com.google.android.maps.MapView} * in conjunction with the {@link com.hardware.SensorManager} * <h3>MapViewCompassDemo</h3><p>This demonstrates creating a Map based Activity.</p><h4>Source files</h4> * <table class="LinkTable"> *         <tr> *             <td >src/com.example.android.apis/view/MapViewCompassDemo.java</td> *             <td >The Alert Dialog Samples implementation</td> *         </tr> * </table> */public class MapViewCompassDemo extends MapActivity {    private static final String TAG = "MapViewCompassDemo";    private SensorManager mSensorManager;    private RotateView mRotateView;    private MapView mMapView;    private MyLocationOverlay mMyLocationOverlay;    private class RotateView extends ViewGroup implements SensorListener {        private static final float SQ2 = 1.414213562373095f;        private final SmoothCanvas mCanvas = new SmoothCanvas();        private float mHeading = 0;        public RotateView(Context context) {            super(context);        }        public void onSensorChanged(int sensor, float[] values) {            //Log.d(TAG, "x: " + values[0] + "y: " + values[1] + "z: " + values[2]);            synchronized (this) {                mHeading = values[0];                invalidate();            }        }        @Override        protected void dispatchDraw(Canvas canvas) {            canvas.save(Canvas.MATRIX_SAVE_FLAG);            canvas.rotate(-mHeading, getWidth() * 0.5f, getHeight() * 0.5f);            mCanvas.delegate = canvas;            super.dispatchDraw(mCanvas);            canvas.restore();        }        @Override        protected void onLayout(boolean changed, int l, int t, int r, int b) {            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) {            int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);            int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);            int sizeSpec;            if (w > h) {                sizeSpec = MeasureSpec.makeMeasureSpec((int) (w * SQ2), MeasureSpec.EXACTLY);            } else {                sizeSpec = MeasureSpec.makeMeasureSpec((int) (h * 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 ev) {            // TODO: rotate events too            return super.dispatchTouchEvent(ev);        }        public void onAccuracyChanged(int sensor, int accuracy) {            // TODO Auto-generated method stub                    }    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);        mRotateView = new RotateView(this);        mMapView = new MapView(this, "MapViewCompassDemo_DummyAPIKey");        mRotateView.addView(mMapView);        setContentView(mRotateView);        mMyLocationOverlay = new MyLocationOverlay(this, mMapView);        mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {            mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());        }});        mMapView.getOverlays().add(mMyLocationOverlay);        mMapView.getController().setZoom(18);        mMapView.setClickable(true);        mMapView.setEnabled(true);    }    @Override    protected void onResume() {        super.onResume();        mSensorManager.registerListener(mRotateView,                SensorManager.SENSOR_ORIENTATION,                SensorManager.SENSOR_DELAY_UI);        mMyLocationOverlay.enableMyLocation();    }    @Override    protected void onStop() {        mSensorManager.unregisterListener(mRotateView);        mMyLocationOverlay.disableMyLocation();        super.onStop();    }    @Override    protected boolean isRouteDisplayed() {        return false;    }    static final class SmoothCanvas extends Canvas {        Canvas delegate;        private final Paint mSmooth = new Paint(Paint.FILTER_BITMAP_FLAG);        public void setBitmap(Bitmap bitmap) {            delegate.setBitmap(bitmap);        }        public void setViewport(int width, int height) {            delegate.setViewport(width, height);        }        public boolean isOpaque() {            return delegate.isOpaque();        }        public int getWidth() {            return delegate.getWidth();        }        public int getHeight() {            return delegate.getHeight();        }        public int save() {            return delegate.save();        }        public int save(int saveFlags) {            return delegate.save(saveFlags);        }        public int saveLayer(RectF bounds, Paint paint, int saveFlags) {            return delegate.saveLayer(bounds, paint, saveFlags);        }        public int saveLayer(float left, float top, float right, float                bottom, Paint paint,                int saveFlags) {            return delegate.saveLayer(left, top, right, bottom, paint,                    saveFlags);        }        public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {            return delegate.saveLayerAlpha(bounds, alpha, saveFlags);        }        public int saveLayerAlpha(float left, float top, float right,                float bottom, int alpha,                int saveFlags) {            return delegate.saveLayerAlpha(left, top, right, bottom,                    alpha, saveFlags);        }        public void restore() {            delegate.restore();        }        public int getSaveCount() {            return delegate.getSaveCount();        }        public void restoreToCount(int saveCount) {            delegate.restoreToCount(saveCount);        }        public void translate(float dx, float dy) {            delegate.translate(dx, dy);        }        public void scale(float sx, float sy) {            delegate.scale(sx, sy);        }        public void rotate(float degrees) {            delegate.rotate(degrees);        }        public void skew(float sx, float sy) {            delegate.skew(sx, sy);        }        public void concat(Matrix matrix) {            delegate.concat(matrix);        }        public void setMatrix(Matrix matrix) {            delegate.setMatrix(matrix);        }        public void getMatrix(Matrix ctm) {            delegate.getMatrix(ctm);        }        public boolean clipRect(RectF rect, Region.Op op) {            return delegate.clipRect(rect, op);        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -