Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Fixes and tweaks #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@
android:versionCode="2"
android:versionName="1.1" >

<uses-sdk android:minSdkVersion="7" />

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17"
/>
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:label="@string/app_name"
android:allowBackup="true"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
32 changes: 0 additions & 32 deletions gen/uk/co/jarofgreen/JustADamnCompass/R.java

This file was deleted.

Binary file added res/drawable-hdpi/background.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/background_black.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/compass_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/compass_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion res/layout/about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<TextView
android:id="@+id/about_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Expand Down
2 changes: 2 additions & 0 deletions res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/invert"
android:title="@string/invert_menu" />
<item android:id="@+id/about"
android:title="@string/about_menu" />
</menu>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Just A Damn Compass</string>
<string name="invert_menu">Invert</string>
<string name="about_menu">About</string>
<string name="about_text">
This app is just a compass.
Expand Down
112 changes: 98 additions & 14 deletions src/uk/co/jarofgreen/JustADamnCompass/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);

}

Expand All @@ -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:
Expand Down