diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e9ebb74..ccdbc0f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,15 +4,27 @@ android:versionCode="2" android:versionName="1.1" > - - + + + android:label="@string/app_name" + android:allowBackup="true" + > + android:screenOrientation="portrait" + > diff --git a/gen/uk/co/jarofgreen/JustADamnCompass/R.java b/gen/uk/co/jarofgreen/JustADamnCompass/R.java deleted file mode 100644 index 8dec15d..0000000 --- a/gen/uk/co/jarofgreen/JustADamnCompass/R.java +++ /dev/null @@ -1,32 +0,0 @@ -/* AUTO-GENERATED FILE. DO NOT MODIFY. - * - * This class was automatically generated by the - * aapt tool from the resource data it found. It - * should not be modified by hand. - */ - -package uk.co.jarofgreen.JustADamnCompass; - -public final class R { - public static final class attr { - } - public static final class drawable { - public static final int compass=0x7f020000; - public static final int ic_launcher=0x7f020001; - } - public static final class id { - public static final int about=0x7f060001; - public static final int about_text=0x7f060000; - } - public static final class layout { - public static final int about=0x7f030000; - } - public static final class menu { - public static final int main=0x7f050000; - } - public static final class string { - public static final int about_menu=0x7f040001; - public static final int about_text=0x7f040002; - public static final int app_name=0x7f040000; - } -} diff --git a/res/drawable-hdpi/background.gif b/res/drawable-hdpi/background.gif new file mode 100644 index 0000000..9b4d145 Binary files /dev/null and b/res/drawable-hdpi/background.gif differ diff --git a/res/drawable-hdpi/background_black.gif b/res/drawable-hdpi/background_black.gif new file mode 100644 index 0000000..41835b2 Binary files /dev/null and b/res/drawable-hdpi/background_black.gif differ diff --git a/res/drawable-hdpi/compass_black.png b/res/drawable-hdpi/compass_black.png new file mode 100644 index 0000000..8c21ae8 Binary files /dev/null and b/res/drawable-hdpi/compass_black.png differ diff --git a/res/drawable-hdpi/compass_white.png b/res/drawable-hdpi/compass_white.png new file mode 100644 index 0000000..a6f2225 Binary files /dev/null and b/res/drawable-hdpi/compass_white.png differ diff --git a/res/drawable-hdpi/ic_launcher.png b/res/drawable-hdpi/ic_launcher.png index 65f2016..f82a44a 100644 Binary files a/res/drawable-hdpi/ic_launcher.png and b/res/drawable-hdpi/ic_launcher.png differ diff --git a/res/drawable-mdpi/ic_launcher.png b/res/drawable-mdpi/ic_launcher.png index 8889e59..a8a3031 100644 Binary files a/res/drawable-mdpi/ic_launcher.png and b/res/drawable-mdpi/ic_launcher.png differ diff --git a/res/drawable-xhdpi/ic_launcher.png b/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..635069f Binary files /dev/null and b/res/drawable-xhdpi/ic_launcher.png differ diff --git a/res/drawable-xxhdpi/ic_launcher.png b/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..533b199 Binary files /dev/null and b/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/res/layout/about.xml b/res/layout/about.xml index 19b964c..fa11977 100644 --- a/res/layout/about.xml +++ b/res/layout/about.xml @@ -4,7 +4,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent"> - + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 28af6ce..96c5585 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,6 +1,7 @@ Just A Damn Compass + Invert About This app is just a compass. diff --git a/src/uk/co/jarofgreen/JustADamnCompass/MainActivity.java b/src/uk/co/jarofgreen/JustADamnCompass/MainActivity.java index 903ac7d..82c4953 100644 --- a/src/uk/co/jarofgreen/JustADamnCompass/MainActivity.java +++ b/src/uk/co/jarofgreen/JustADamnCompass/MainActivity.java @@ -3,13 +3,18 @@ import uk.co.jarofgreen.JustADamnCompass.AboutActivity; import android.app.Activity; import android.os.Bundle; +import android.preference.PreferenceManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.graphics.*; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.util.Log; +import android.view.Display; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -20,26 +25,49 @@ public class MainActivity extends Activity { private SensorManager mSensorManager; private Sensor mSensor; private float[] mValues; - + private float lastDirection = -360; + private float secondLastDirection = -360; private SampleView mView; + + private final int MIN_ROTATION = 2; private final SensorEventListener mListener = new SensorEventListener() { public void onSensorChanged(SensorEvent event) { + mValues = event.values; - if (mView != null) mView.invalidate(); + // check for min rotation or jitter + + if(Math.abs(lastDirection - mValues[0]) < MIN_ROTATION || secondLastDirection == mValues[0]) + return; + + secondLastDirection = lastDirection; + lastDirection = mValues[0]; + + if (mView != null) mView.invalidate(); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; + private int w; + private int h; + private String TAG = "Compass"; + private SharedPreferences prefs; + private static boolean invert; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); + + prefs = PreferenceManager.getDefaultSharedPreferences(this); + invert = prefs.getBoolean("invert", false); + mView = new SampleView(this); + mView.setBackgroundColor(invert?Color.WHITE:Color.BLACK); setContentView(mView); + } @Override @@ -56,27 +84,72 @@ protected void onStop() { private class SampleView extends View { + private Bitmap backgroundImage; private Bitmap compassImage; + private int nWidth; + private int nHeight; + private Matrix matrix = new Matrix(); + private Matrix rotator = new Matrix(); + private Bitmap a; + private Bitmap b; public SampleView(Context context) { super(context); - compassImage = BitmapFactory.decodeResource(getResources(), R.drawable.compass); + if(invert) { + a = BitmapFactory.decodeResource(getResources(), R.drawable.background); + b = BitmapFactory.decodeResource(getResources(), R.drawable.compass_black); + } + else { + a = BitmapFactory.decodeResource(getResources(), R.drawable.background_black); + b = BitmapFactory.decodeResource(getResources(), R.drawable.compass_white); + } } @Override protected void onDraw(Canvas canvas) { - canvas.drawColor(Color.BLACK); + // center + + canvas.translate(0, h/2-nHeight/2); + + // rotate - int w = canvas.getWidth(); - int h = canvas.getHeight(); - int cx = w / 2; - int cy = h / 2; - - canvas.translate(cx, cy); if (mValues != null) { - canvas.rotate(-mValues[0]); + rotator.setRotate(-mValues[0], nWidth/2, nHeight/2); } - canvas.drawBitmap(compassImage, -150, -150, null); + canvas.drawBitmap(backgroundImage, matrix, null); + canvas.drawBitmap(compassImage, rotator, null); + + + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + // variables + + w = MeasureSpec.getSize(widthMeasureSpec); + h = MeasureSpec.getSize(heightMeasureSpec); + + Log.d(TAG,"width: "+w+" height: "+h); + + int mHeight = b.getHeight(); + int mWidth = b.getWidth(); + + nWidth = w; + nHeight = h; + + if(mHeight/mWidth > h/w) { // image skinnier than canvas + nWidth = (int) (mWidth*((float)h/(float)mHeight)); + } + else { // image fatter than or equal to canvas + nHeight = (int) (mHeight*((float)w/(float)mWidth)); + } + + Log.d(TAG,"image width: "+nWidth+" image height: "+nHeight); + + backgroundImage = Bitmap.createScaledBitmap(a, nWidth, nHeight, false); + compassImage = Bitmap.createScaledBitmap(b, nWidth, nHeight, false); } @@ -91,9 +164,20 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { + Intent i; + switch (item.getItemId()) { case R.id.about: - Intent i = new Intent(this, AboutActivity.class); + i = new Intent(this, AboutActivity.class); + startActivity(i); + return true; + case R.id.invert: + // invert then restart + Editor e = prefs.edit(); + e.putBoolean("invert", invert?false:true); + e.commit(); + + i = new Intent(this, MainActivity.class); + i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); return true; default: