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.Log; 27 import android.view.View; 28 29 public class Compass extends GraphicsActivity { 30 31 private static final String TAG = "Compass"; 32 33 private SensorManager mSensorManager; 34 private Sensor mSensor; 35 private SampleView mView; 36 private float[] mValues; 37 38 private final SensorEventListener mListener = new SensorEventListener() { 39 public void onSensorChanged(SensorEvent event) { 40 if (false) Log.d(TAG, 41 "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")"); 42 mValues = event.values; 43 if (mView != null) { 44 mView.invalidate(); 45 } 46 } 47 48 public void onAccuracyChanged(Sensor sensor, int accuracy) { 49 } 50 }; 51 52 @Override onCreate(Bundle icicle)53 protected void onCreate(Bundle icicle) { 54 super.onCreate(icicle); 55 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 56 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 57 mView = new SampleView(this); 58 setContentView(mView); 59 } 60 61 @Override onResume()62 protected void onResume() 63 { 64 if (false) Log.d(TAG, "onResume"); 65 super.onResume(); 66 67 mSensorManager.registerListener(mListener, mSensor, 68 SensorManager.SENSOR_DELAY_GAME); 69 } 70 71 @Override onStop()72 protected void onStop() 73 { 74 if (false) Log.d(TAG, "onStop"); 75 mSensorManager.unregisterListener(mListener); 76 super.onStop(); 77 } 78 79 private class SampleView extends View { 80 private Paint mPaint = new Paint(); 81 private Path mPath = new Path(); 82 private boolean mAnimate; 83 SampleView(Context context)84 public SampleView(Context context) { 85 super(context); 86 87 // Construct a wedge-shaped path 88 mPath.moveTo(0, -50); 89 mPath.lineTo(-20, 60); 90 mPath.lineTo(0, 50); 91 mPath.lineTo(20, 60); 92 mPath.close(); 93 } 94 onDraw(Canvas canvas)95 @Override protected void onDraw(Canvas canvas) { 96 Paint paint = mPaint; 97 98 canvas.drawColor(Color.WHITE); 99 100 paint.setAntiAlias(true); 101 paint.setColor(Color.BLACK); 102 paint.setStyle(Paint.Style.FILL); 103 104 int w = canvas.getWidth(); 105 int h = canvas.getHeight(); 106 int cx = w / 2; 107 int cy = h / 2; 108 109 canvas.translate(cx, cy); 110 if (mValues != null) { 111 canvas.rotate(-mValues[0]); 112 } 113 canvas.drawPath(mPath, mPaint); 114 } 115 116 @Override onAttachedToWindow()117 protected void onAttachedToWindow() { 118 mAnimate = true; 119 if (false) Log.d(TAG, "onAttachedToWindow. mAnimate=" + mAnimate); 120 super.onAttachedToWindow(); 121 } 122 123 @Override onDetachedFromWindow()124 protected void onDetachedFromWindow() { 125 mAnimate = false; 126 if (false) Log.d(TAG, "onDetachedFromWindow. mAnimate=" + mAnimate); 127 super.onDetachedFromWindow(); 128 } 129 } 130 } 131