Reformat to Android style guidelines

Change-Id: Ib9d7e39464a246dbaae38e00fbb325f153f89f65
This commit is contained in:
Mathias Agopian
2010-08-16 16:13:24 -07:00
parent a7c85d9235
commit a37be42f9f

View File

@@ -72,8 +72,8 @@ public class AccelerometerPlayActivity extends Activity {
mDisplay = mWindowManager.getDefaultDisplay(); mDisplay = mWindowManager.getDefaultDisplay();
// Create a bright wake lock // Create a bright wake lock
mWakeLock = mPowerManager.newWakeLock( mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName()); .getName());
// instantiate our simulation view and set it as the activity's content // instantiate our simulation view and set it as the activity's content
mSimulationView = new SimulationView(this); mSimulationView = new SimulationView(this);
@@ -112,8 +112,7 @@ public class AccelerometerPlayActivity extends Activity {
class SimulationView extends View implements SensorEventListener { class SimulationView extends View implements SensorEventListener {
// diameter of the balls in meters // diameter of the balls in meters
private static final float sBallDiameter = 0.004f; private static final float sBallDiameter = 0.004f;
private static final float sBallDiameter2 = sBallDiameter private static final float sBallDiameter2 = sBallDiameter * sBallDiameter;
* sBallDiameter;
// friction of the virtual table and air // friction of the virtual table and air
private static final float sFriction = 0.1f; private static final float sFriction = 0.1f;
@@ -166,39 +165,28 @@ public class AccelerometerPlayActivity extends Activity {
final float gy = -sy * m; final float gy = -sy * m;
/* /*
* <20>F = mA <=> A = <20>F / m * <20>F = mA <=> A = <20>F / m We could simplify the code by
* * completely eliminating "m" (the mass) from all the equations,
* We could simplify the code by completely eliminating "m" (the * but it would hide the concepts from this sample code.
* mass) from all the equations, but it would hide the concepts
* from this sample code.
*/ */
final float invm = 1.0f / m; final float invm = 1.0f / m;
final float ax = gx * invm; final float ax = gx * invm;
final float ay = gy * invm; final float ay = gy * invm;
/* /*
* Time-corrected Verlet integration * Time-corrected Verlet integration The position Verlet
* * integrator is defined as x(t+<2B>t) = x(t) + x(t) - x(t-<2D>t) +
* The position Verlet integrator is defined as * a(t)<29>t<EFBFBD>2 However, the above equation doesn't handle variable
* * <20>t very well, a time-corrected version is needed: x(t+<2B>t) =
* x(t+<2B>t) = x(t) + x(t) - x(t-<2D>t) + a(t)<29>t<EFBFBD>2 * x(t) + (x(t) - x(t-<2D>t)) * (<28>t/<2F>t_prev) + a(t)<29>t<EFBFBD>2 We also add
* * a simple friction term (f) to the equation: x(t+<2B>t) = x(t) +
* However, the above equation doesn't handle variable <20>t very * (1-f) * (x(t) - x(t-<2D>t)) * (<28>t/<2F>t_prev) + a(t)<29>t<EFBFBD>2
* well, a time-corrected version is needed:
*
* x(t+<2B>t) = x(t) + (x(t) - x(t-<2D>t)) * (<28>t/<2F>t_prev) + a(t)<29>t<EFBFBD>2
*
*
* We also add a simple friction term (f) to the equation:
*
* x(t+<2B>t) = x(t) + (1-f) * (x(t) - x(t-<2D>t)) * (<28>t/<2F>t_prev) +
* a(t)<29>t<EFBFBD>2
*/ */
final float dTdT = dT * dT; final float dTdT = dT * dT;
final float x = mPosX + mOneMinusFriction * dTC final float x = mPosX + mOneMinusFriction * dTC * (mPosX - mLastPosX) + mAccelX
* (mPosX - mLastPosX) + mAccelX * dTdT; * dTdT;
final float y = mPosY + mOneMinusFriction * dTC final float y = mPosY + mOneMinusFriction * dTC * (mPosY - mLastPosY) + mAccelY
* (mPosY - mLastPosY) + mAccelY * dTdT; * dTdT;
mLastPosX = mPosX; mLastPosX = mPosX;
mLastPosY = mPosY; mLastPosY = mPosY;
mPosX = x; mPosX = x;
@@ -254,8 +242,7 @@ public class AccelerometerPlayActivity extends Activity {
private void updatePositions(float sx, float sy, long timestamp) { private void updatePositions(float sx, float sy, long timestamp) {
final long t = timestamp; final long t = timestamp;
if (mLastT != 0) { if (mLastT != 0) {
final float dT = (float) (t - mLastT) final float dT = (float) (t - mLastT) * (1.0f / 1000000000.0f);
* (1.0f / 1000000000.0f);
if (mLastDeltaT != 0) { if (mLastDeltaT != 0) {
final float dTC = dT / mLastDeltaT; final float dTC = dT / mLastDeltaT;
final int count = mBalls.length; final int count = mBalls.length;
@@ -309,8 +296,7 @@ public class AccelerometerPlayActivity extends Activity {
dd = dx * dx + dy * dy; dd = dx * dx + dy * dy;
// simulate the spring // simulate the spring
final float d = (float) Math.sqrt(dd); final float d = (float) Math.sqrt(dd);
final float c = (0.5f * (sBallDiameter - d)) final float c = (0.5f * (sBallDiameter - d)) / d;
/ d;
curr.mPosX -= dx * c; curr.mPosX -= dx * c;
curr.mPosY -= dy * c; curr.mPosY -= dy * c;
ball.mPosX += dx * c; ball.mPosX += dx * c;
@@ -348,8 +334,7 @@ public class AccelerometerPlayActivity extends Activity {
* of the acceleration. As an added benefit, we use less power and * of the acceleration. As an added benefit, we use less power and
* CPU resources. * CPU resources.
*/ */
mSensorManager.registerListener(this, mAccelerometer, mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
SensorManager.SENSOR_DELAY_UI);
} }
public void stopSimulation() { public void stopSimulation() {
@@ -358,8 +343,7 @@ public class AccelerometerPlayActivity extends Activity {
public SimulationView(Context context) { public SimulationView(Context context) {
super(context); super(context);
mAccelerometer = mSensorManager mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
DisplayMetrics metrics = new DisplayMetrics(); DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics); getWindowManager().getDefaultDisplay().getMetrics(metrics);
@@ -369,18 +353,15 @@ public class AccelerometerPlayActivity extends Activity {
mMetersToPixelsY = mYDpi / 0.0254f; mMetersToPixelsY = mYDpi / 0.0254f;
// rescale the ball so it's about 0.5 cm on screen // rescale the ball so it's about 0.5 cm on screen
Bitmap ball = BitmapFactory.decodeResource(getResources(), Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
R.drawable.ball);
final int dstWidth = (int) (sBallDiameter * mMetersToPixelsX + 0.5f); final int dstWidth = (int) (sBallDiameter * mMetersToPixelsX + 0.5f);
final int dstHeight = (int) (sBallDiameter * mMetersToPixelsY + 0.5f); final int dstHeight = (int) (sBallDiameter * mMetersToPixelsY + 0.5f);
mBitmap = Bitmap mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
.createScaledBitmap(ball, dstWidth, dstHeight, true);
Options opts = new Options(); Options opts = new Options();
opts.inDither = true; opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565; opts.inPreferredConfig = Bitmap.Config.RGB_565;
mWood = BitmapFactory.decodeResource(getResources(), mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood, opts);
R.drawable.wood, opts);
} }
@Override @Override
@@ -400,13 +381,10 @@ public class AccelerometerPlayActivity extends Activity {
/* /*
* record the accelerometer data, the event's timestamp as well as * record the accelerometer data, the event's timestamp as well as
* the current time. The latter is needed so we can calculate the * the current time. The latter is needed so we can calculate the
* "present" time during rendering. * "present" time during rendering. In this application, we need to
* * take into account how the screen is rotated with respect to the
* In this application, we need to take into account how the * sensors (which always return data in a coordinate space aligned
* screen is rotated with respect to the sensors (which always * to with the screen in its native orientation).
* return data in a coordinate space aligned to with the screen
* in its native orientation).
*
*/ */
switch (mDisplay.getRotation()) { switch (mDisplay.getRotation()) {
@@ -447,8 +425,7 @@ public class AccelerometerPlayActivity extends Activity {
*/ */
final ParticleSystem particleSystem = mParticleSystem; final ParticleSystem particleSystem = mParticleSystem;
final long now = mSensorTimeStamp final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);
+ (System.nanoTime() - mCpuTimeStamp);
final float sx = mSensorX; final float sx = mSensorX;
final float sy = mSensorY; final float sy = mSensorY;