Ok, so I wrote an app tonight that reads the 'Roll' data from the
compass and levels a line
on the screen. (It's on marketplace. It's also here:
http://www.imakmud.com/level.apk
) --Source Code Below:
Here's how it works:
-Basically every time the compass data changes, the canvas is
rotated and redrawn. It's very fast I think.
Problems I'm experiencing:
What I've got there is a single view with a single canvas. I'd like to
incorporate some
static elements like a pointers that don't rotate with the rest of the
canvas.
I experimented with drawables, but I'm just not sure that's the
answer.
So, I'm looking to have multiple graphics on the screen. One rotates,
the others don't.
Any direction is appreciated. --God this device is awesome!
Here is the Simple Level Code with comments as I understand the
program to be working:
(Please correct me where I am wrong.) I hope it helps other, and I
hope other developers are
able to chime in.
-----Look for the //LOOK HERE comment.-----
--ALL IMPORTS CORRECT--
public class level extends Activity {
private static final String TAG = "Compass";
private Bitmap mBitmap;
private SensorManager mSensorManager;
private SampleView mView;
private float[] mValues;
//SensorListener is called by the SensorManager object returned in
OnCreate
private final SensorListener mListener = new SensorListener() {
public void onSensorChanged(int sensor, float[] values) {
if (Config.LOGD) Log.d(TAG, "sensorChanged (" + values[0]
+ ", " + values[1] + ", " + values[2] + ")");
//mValues contains direction, yaw, and roll.
mValues = values;
if (mView != null) {
mView.invalidate();
}
}
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
mView = new SampleView(this);
//SampleView(); is called here. So is onDraw?
//Question: if a view is defined here through an xml file, is
onDraw still called by default?
// Is that even right to assume onDraw is called here?
setContentView(mView);
}
@Override
protected void onResume()
{
if (Config.LOGD) Log.d(TAG, "onResume");
super.onResume();
mSensorManager.registerListener(mListener,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop()
{
if (Config.LOGD) Log.d(TAG, "onStop");
mSensorManager.unregisterListener(mListener);
super.onStop();
}
private class SampleView extends View {
private Paint mPaint = new Paint();
private Path mPath = new Path();
private boolean mAnimate;
private long mNextTime;
public SampleView(Context context) {
super(context);
//Maybe onDraw is called at this point?
}
//Because mView.invalidate is called in SensorListener, we can count
on onDraw everytime
//there is a change of state.
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
canvas.drawColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
if (mValues != null) {
float rotateme = mValues[2];
//LOOK HERE
//Can I put two canvases in action?
//Should I use a drawable for two separately rotating
objects?
//??? --Any advice is appreciated.
canvas.rotate(rotateme, 160, 240 );
}
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.line2);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}
@Override
protected void onAttachedToWindow() {
mAnimate = true;
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
mAnimate = false;
super.onDetachedFromWindow();
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---