Created
July 25, 2013 03:03
-
-
Save jianshelu/6076598 to your computer and use it in GitHub Desktop.
MoveButton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dbutton.movebutton; | |
import android.content.Context; | |
import android.graphics.*; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.os.SystemClock; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
public class MainActivity extends Activity { | |
private SensorManager mSensorManager; | |
private GraphView mGraphView; | |
private class GraphView extends View implements SensorEventListener{ | |
private final int[] mColors = new int[1]; | |
private Bitmap mBitmap; | |
private Paint mPaint = new Paint(); | |
private Canvas mCanvas = new Canvas(); | |
private RectF mRect = new RectF(); | |
private float mYOffset; | |
private float[] mScale = new float[2]; | |
private float mWidth; | |
private float mHeight; | |
private float mMaxX; | |
private float mMaxY; | |
private float mLastx; | |
private float mLasty; | |
private float[] mLastValues; | |
private String TAG; | |
private float plusY = 0.0f; | |
private float mScaleX; | |
private float mScaleY; | |
private float mSpeed = 1.0f; | |
private float[] mGravity = { 0.0f, 0.0f, 0.0f }; | |
private float[] mLinearAcceleration = { 0.0f, 0.0f, 0.0f }; | |
private static final int X = 0; | |
private static final int Y = 1; | |
private static final int Z = 2; | |
private float lastTime ; | |
private float[] lastVels = {0.0f,0.0f,0.0f}; | |
public GraphView(Context context) { | |
super(context); | |
mColors[0] = Color.argb(192, 255, 64, 64); | |
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); | |
} | |
protected void onSizeChanged(int w, int h, int oldW, int oldH) { | |
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); | |
mCanvas.setBitmap(mBitmap); | |
mCanvas.drawColor(0xFFFFFFFF); | |
mYOffset = h * 0.5f; | |
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.GRAVITY_EARTH * 2))); | |
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX))); | |
mScaleX = - (w * 0.5f * (1.0f / (SensorManager.GRAVITY_EARTH * 2))/1000); | |
mScaleY = - (h * 0.5f * (1.0f / (SensorManager.GRAVITY_EARTH * 2))/1000); | |
mWidth = w; | |
mHeight = h; | |
if (mWidth < mHeight) { | |
mMaxX = w; | |
mMaxY = h; | |
} else { | |
mMaxX = w - 50; | |
} | |
mLastx = mMaxX; | |
mLasty = mYOffset; | |
super.onSizeChanged(w, h, oldW, oldH); | |
} | |
protected void onDraw(Canvas canvas) { | |
synchronized (this) { | |
if (mBitmap != null) { | |
final Paint paint = mPaint; | |
if (mLastx >= mMaxX) { | |
mLastx = 0; | |
final Canvas cavas = mCanvas; | |
final float yoffset = mYOffset; | |
final float maxx = mMaxX; | |
final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0]; | |
paint.setColor(0xFFAAAAAA); | |
cavas.drawColor(0xFFFFFFFF); | |
cavas.drawLine(0, yoffset, maxx, yoffset, paint); | |
cavas.drawLine(0, yoffset - oneG, maxx, yoffset - oneG, paint); | |
cavas.drawLine(0, yoffset + oneG, maxx, yoffset + oneG, paint); | |
} | |
canvas.drawBitmap(mBitmap, 0, 0, null); | |
} | |
} | |
} | |
@Override | |
public void onSensorChanged(SensorEvent sensorEvent) { | |
synchronized (this) { | |
if (mBitmap != null) { | |
final Canvas canvas = mCanvas; | |
final Paint paint = mPaint; | |
if (mLasty >= mMaxY) { | |
mLasty = mYOffset; | |
} | |
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
float deltaX = 1.0f; | |
float newX = deltaX; | |
float newY = mLasty + mSpeed; | |
float[] vals = sensorEvent.values.clone(); | |
// float[] vals = setCurrentAcceleration(sensorEvent); | |
float[] velocitys = toVelocity(sensorEvent); | |
for (int i = 0; i < 1; i++) { | |
final float vx = mLastx + velocitys[0] * mScaleX; | |
final float vy = mYOffset + velocitys[1] * mScaleY; | |
final float dy = mYOffset + mSpeed; | |
paint.setColor(mColors[0]); | |
canvas.drawLine(mLastx,mYOffset,mLastx,vy,paint); | |
Log.d(TAG, "x: " + vals[0] + " , y: " + vals[1] + " ; velocityx: " + | |
velocitys[0] + " , velocityy: " + velocitys[1]); | |
mLastx += mSpeed; | |
mLasty = vy; | |
plusY += 2.0f; | |
// mLastValues[i] = v; | |
} | |
} | |
} | |
invalidate(); | |
} | |
} | |
private float[] toVelocity(SensorEvent sensorEvent) { | |
float[] vels = {0.0f,0.0f,0.0f}; | |
// float[] filters = setCurrentAcceleration(sensorEvent); | |
float[] filters = sensorEvent.values.clone(); | |
float dt = (sensorEvent.timestamp - lastTime)/1000000000.0f; | |
for (int i = 0; i < 3; i++) { | |
vels[i] = lastVels[i] + filters[i] * dt ; | |
} | |
Log.d(TAG, "lastTime: " + lastTime + ", timestamp: " + sensorEvent.timestamp | |
+ " ,dt: " + dt); | |
lastTime = sensorEvent.timestamp; | |
lastVels = vels.clone(); | |
return vels; | |
} | |
private float[] setCurrentAcceleration(SensorEvent event) { | |
/* | |
* BEGIN SECTION from Android developer site. This code accounts for | |
* gravity using a high-pass filter | |
*/ | |
// alpha is calculated as t / (t + dT) | |
// with t, the low-pass filter's time-constant | |
// and dT, the event delivery rate | |
final float alpha = 0.8f; | |
// Gravity components of x, y, and z acceleration | |
mGravity[X] = alpha * mGravity[X] + (1 - alpha) * event.values[X]; | |
mGravity[Y] = alpha * mGravity[Y] + (1 - alpha) * event.values[Y]; | |
mGravity[Z] = alpha * mGravity[Z] + (1 - alpha) * event.values[Z]; | |
// Linear acceleration along the x, y, and z axes (gravity effects removed) | |
mLinearAcceleration[X] = event.values[X] - mGravity[X]; | |
mLinearAcceleration[Y] = event.values[Y] - mGravity[Y]; | |
mLinearAcceleration[Z] = event.values[Z] - mGravity[Z]; | |
return mLinearAcceleration; | |
/* | |
* END SECTION from Android developer site | |
*/ | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int i) { | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |
mGraphView = new GraphView(this); | |
setContentView(mGraphView); | |
} | |
protected void onResume() { | |
super.onResume(); | |
mSensorManager.registerListener(mGraphView, mSensorManager.getDefaultSensor( | |
Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST); | |
} | |
protected void onStop() { | |
mSensorManager.unregisterListener(mGraphView); | |
onStop(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment