1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 import android.content.Context; 20 import android.graphics.*; 21 import android.hardware.Sensor; 22 import android.hardware.SensorEvent; 23 import android.hardware.SensorEventListener; 24 import android.hardware.SensorManager; 25 import android.os.Bundle; 26 import android.util.Config; 27 import android.util.Log; 28 import android.view.View; 29 30 public class Compass extends GraphicsActivity { 31 32 private static final String TAG = "Compass"; 33 34 private SensorManager mSensorManager; 35 private Sensor mSensor; 36 private SampleView mView; 37 private float[] mValues; 38 39 private final SensorEventListener mListener = new SensorEventListener() { 40 public void onSensorChanged(SensorEvent event) { 41 if (Config.LOGD) Log.d(TAG, 42 "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")"); 43 mValues = event.values; 44 if (mView != null) { 45 mView.invalidate(); 46 } 47 } 48 49 public void onAccuracyChanged(Sensor sensor, int accuracy) { 50 } 51 }; 52 53 @Override onCreate(Bundle icicle)54 protected void onCreate(Bundle icicle) { 55 super.onCreate(icicle); 56 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 57 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 58 mView = new SampleView(this); 59 setContentView(mView); 60 } 61 62 @Override onResume()63 protected void onResume() 64 { 65 if (Config.LOGD) Log.d(TAG, "onResume"); 66 super.onResume(); 67 68 mSensorManager.registerListener(mListener, mSensor, 69 SensorManager.SENSOR_DELAY_GAME); 70 } 71 72 @Override onStop()73 protected void onStop() 74 { 75 if (Config.LOGD) Log.d(TAG, "onStop"); 76 mSensorManager.unregisterListener(mListener); 77 super.onStop(); 78 } 79 80 private class SampleView extends View { 81 private Paint mPaint = new Paint(); 82 private Path mPath = new Path(); 83 private boolean mAnimate; 84 private long mNextTime; 85 SampleView(Context context)86 public SampleView(Context context) { 87 super(context); 88 89 // Construct a wedge-shaped path 90 mPath.moveTo(0, -50); 91 mPath.lineTo(-20, 60); 92 mPath.lineTo(0, 50); 93 mPath.lineTo(20, 60); 94 mPath.close(); 95 } 96 onDraw(Canvas canvas)97 @Override protected void onDraw(Canvas canvas) { 98 Paint paint = mPaint; 99 100 canvas.drawColor(Color.WHITE); 101 102 paint.setAntiAlias(true); 103 paint.setColor(Color.BLACK); 104 paint.setStyle(Paint.Style.FILL); 105 106 int w = canvas.getWidth(); 107 int h = canvas.getHeight(); 108 int cx = w / 2; 109 int cy = h / 2; 110 111 canvas.translate(cx, cy); 112 if (mValues != null) { 113 canvas.rotate(-mValues[0]); 114 } 115 canvas.drawPath(mPath, mPaint); 116 } 117 118 @Override onAttachedToWindow()119 protected void onAttachedToWindow() { 120 mAnimate = true; 121 super.onAttachedToWindow(); 122 } 123 124 @Override onDetachedFromWindow()125 protected void onDetachedFromWindow() { 126 mAnimate = false; 127 super.onDetachedFromWindow(); 128 } 129 } 130 } 131 132