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:
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(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. CursorAdapter vs ResourceCursorAdapter

Hi,

What is the exact difference between CursorAdapter and
ResourceCursorAdapter ?
Can somebody explain what are the api's that are required to override
when implementing my own ResourceCursorAdapter ?

 I have gone through the ResourceCursorAdapter documentation and able
to figure out that it's constructor takes an additional layout
parameter as compared to CursorAdapter constructor. But I am unable to
understand what is the significance of having an additional layout
parameter in ResourceCusorAdapter constructor.

Thanks,
Parag

-- 

2. Froyo On Beagle

Hi All,
                 Anyone ported the Froyo on Beagle Board.
Kindly guide me.

with regards,
saminath

-- 

3. GridView Problem

4. NFS mount issue in Android

5. unable to play rtsp stream in htc hero mobile

6. Why died Internet (Chrome) on Android 2.1 (HTC Desire)

7. Video Buffering Problems