Simple Level App

by joshbeck » Tue, 04 Nov 2008 15:53:59 GMT

Sponsored Links
 k, 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:
) --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
I experimented with drawables, but I'm just not sure that's the

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.-----


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

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) {

public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub


protected void onCreate(Bundle icicle) {
mSensorManager =
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?


protected void onResume()
if (Config.LOGD) Log.d(TAG, "onResume");

protected void onStop()
if (Config.LOGD) Log.d(TAG, "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) {
//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(Ca

Simple Level App

by Steve Oldmeadow » Tue, 04 Nov 2008 16:26:51 GMT

 All you have to do is reverse the rotation e.g rotate by -rotateMe
after you have drawn the rotated image and then all subsequent draws
will be non rotated.


Sponsored Links

Other Threads

1. wrapping strings or newlines - optionsmenu - condensed title

I am using a resource file to set onCreateOptionsMenu button titles.
Is it possible to create a line break, newline or wrap the text of the
button title?

Here is an altered example in res/values/string.xml in the Notepadv1
<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">Notepad v1</string>
    <string name="no_notes">No Notes Yet</string>
    <string name="menu_insert">Add Item</string>
    <string name="lame_dance">We are dancing and dancing forever and

I would want the string "We are dancing and dancing forever and ever"
to be displayed on the options menu as:
"We are dancing
and dancing
forever and ever"


2. Trouble editing XML files with Eclipse Galileo & SDK 1.6

I have the exact same problem with a fresh install in Ubuntu 9.10.  I
have to close the project and reopen, even though there are no errors
in the xml.  Bizarre!


3. ANDROID_SWT on Vista

4. Mac OSX Kext for Android devices?

5. Need help with new App idea

6. input issue with lunar lander tutorial

7. activating g1 without wifi