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