huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.util.List;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import com.DeviceTest.helper.ControlButtonUtil;
 
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.DeviceTest.view.*;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
 * @author LanBinYuan
 * @date 2011-06-11
 * 
 */
 
public class GyroscopeTestActivity extends Activity {
   /** Called when the activity is first created. */
   private SensorManager sensorManager;
   private SensorEventListener lsn = null;
   private TouchSurfaceView mGLSurfaceView;
    private Button save_btn;
   private double x_offset=0;
   private double y_offset=0;
   private double z_offset=0;
 
   private float x;
     private float y;
     private float z = 0.0F;
     int i = 0;
   protected void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
       
 
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS) + ")");
       //requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
       
       setContentView(R.layout.gyroscopetest);
       ControlButtonUtil.initControlButtonView(this);
       sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
       this.mGLSurfaceView = new TouchSurfaceView(this);
       //setContentView(this.mGLSurfaceView);
       
      // AdView localAdView = new AdView(this);
       //localAdView.setVerticalGravity(80);
      // localRelativeLayout.addView(localAdView, localLayoutParams);
//        addContentView(localRelativeLayout, new WindowManager.LayoutParams(-1, -2));
       RelativeLayout layout2 = (RelativeLayout)findViewById(R.id.relativeLayout2);
       RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
       params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
       layout2.setGravity(Gravity.BOTTOM);
       layout2.addView(mGLSurfaceView, params2);
      // LayoutInflater.from(this).inflate(R.xml.control_buttons, get);
       this.mGLSurfaceView.requestFocus();
       this.mGLSurfaceView.setFocusableInTouchMode(true);
       save_btn = (Button) findViewById(R.id.save_calibration_button);
   }
   
 
    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
        this.sensorManager.unregisterListener(lsn);
    }
 
   
   protected void onResume() {
       super.onResume();
 
       lsn = new SensorEventListener() {
           public void onAccuracyChanged(Sensor sensor, int accuracy) {
               // TODO Auto-generated method stub
 
           }
 
           public void onSensorChanged(SensorEvent e) {
               TextView subTitle = (TextView) findViewById(R.id.Gyroscope);
               subTitle.setTextColor(Color.rgb(255, 0, 0));
 
               String info = "     name:" + String.valueOf(e.sensor.getName());
               info += "\n";
 
               info += "     vendor:" + String.valueOf(e.sensor.getVendor());
               info += "\n";
               info += "     version:" + String.valueOf(e.sensor.getVersion());
               info += "\n";
               info += "     maxRange:"
                       + String.valueOf(e.sensor.getMaximumRange());
               info += "\n";
               info += "     resolution:"
                       + String.valueOf(e.sensor.getResolution());
               info += "\n";
               info += "     power:" + String.valueOf(e.sensor.getPower());
 
               TextView infoView = (TextView) findViewById(R.id.magnetic_info);
               infoView.setText(info);
               // --------------
 
               TextView text = (TextView) findViewById(R.id.magnetic_x);
               text.setText("     x:"
                       + String.valueOf(e.values[SensorManager.DATA_X]));
               text.setTextColor(android.graphics.Color.GREEN);
 
               TextView text2 = (TextView) findViewById(R.id.magnetic_y);
               text2.setText("     y:"
                       + String.valueOf(e.values[SensorManager.DATA_Y]));
               text2.setTextColor(android.graphics.Color.GREEN);
 
               TextView text3 = (TextView) findViewById(R.id.magnetic_z);
               text3.setText("     z:"
                       + String.valueOf(e.values[SensorManager.DATA_Z]));
               text3.setTextColor(android.graphics.Color.GREEN);
               
               x += e.values[0];
               y += e.values[1];
               z += e.values[2];
               mGLSurfaceView.updateGyro(x, y, z);
              
               int j = i;
               i = (j + 1);
               if (j % 50 != 0)
                 return;
               Log.i("gyro", "x=" + e.values[0] + " y=" +e.values[1] + " z=" + e.values[2]);
               x_offset=e.values[0]-0;
               y_offset=e.values[1]-0;
               z_offset=e.values[2]-0;
           }
 
       };
 
       Sensor sensors = sensorManager
               .getDefaultSensor(Sensor.TYPE_GYROSCOPE);
 
       sensorManager.registerListener(lsn, sensors,
               SensorManager.SENSOR_DELAY_NORMAL);
       this.mGLSurfaceView.onResume();
save_btn.setOnClickListener(new OnClickListener()
       {
 
           @Override
           public void onClick(View v)
           {
               // TODO Auto-generated method stub
               File savefile = new File("/cache/Gyroscope.txt");
               if (savefile.exists()) {
                   savefile.delete();
               }
               if (!savefile.exists())
               {
                   try
                   {
                       savefile.createNewFile();
                   } catch (IOException e)
                   {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
               }
               StringBuilder sb = new StringBuilder();
               sb.append("x_offset:" + x_offset).append("\n").append("y_offset:" + y_offset).append("\n").append("z_offset:" + x_offset);
               String sbStirng = sb.toString();
               FileOutputStream fos = null;
               try
               {
                   fos = new FileOutputStream(savefile, false);
                   fos.write(sbStirng.getBytes());
                   fos.close();
               } catch (FileNotFoundException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (IOException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } finally
               {
                   if (fos != null)
                   {
                       try
                       {
                           fos.close();
                       } catch (IOException e)
                       {
                           e.printStackTrace();
                       }
                   }
               }
               Toast.makeText(getApplicationContext(), getString(R.string.save_isok), Toast.LENGTH_SHORT).show();
           }
       });
 
   }
 
   //
   
   protected void onStop() {
       super.onStop();
       sensorManager.unregisterListener(lsn);
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
}