From d292dc9c0e6af5197f514320a0bf7e35711539d9 Mon Sep 17 00:00:00 2001 From: akshaydashrath Date: Fri, 13 Jul 2012 13:19:14 +0100 Subject: [PATCH] -fixing bits of the rotated image view --- CompassExample/AndroidManifest.xml | 104 +++++++++--------- .../res/layout/activity_compass.xml | 26 ++--- .../novoda/example/compass/ImageActivity.java | 68 ------------ .../compass/activities/CompassActivity.java | 13 +++ .../compass/view/RotatedImageView.java | 16 +-- 5 files changed, 76 insertions(+), 151 deletions(-) delete mode 100644 CompassExample/src/com/novoda/example/compass/ImageActivity.java diff --git a/CompassExample/AndroidManifest.xml b/CompassExample/AndroidManifest.xml index bfdc6ed1..c7c7b4c5 100644 --- a/CompassExample/AndroidManifest.xml +++ b/CompassExample/AndroidManifest.xml @@ -1,59 +1,55 @@ + package="com.novoda.example.compass" android:versionCode="1" + android:versionName="1.0"> - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CompassExample/res/layout/activity_compass.xml b/CompassExample/res/layout/activity_compass.xml index a5ebf470..4680b136 100644 --- a/CompassExample/res/layout/activity_compass.xml +++ b/CompassExample/res/layout/activity_compass.xml @@ -1,16 +1,14 @@ - + - + - + + + + diff --git a/CompassExample/src/com/novoda/example/compass/ImageActivity.java b/CompassExample/src/com/novoda/example/compass/ImageActivity.java deleted file mode 100644 index 51598fdc..00000000 --- a/CompassExample/src/com/novoda/example/compass/ImageActivity.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.novoda.example.compass; - -import utils.RotatedImageView; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Matrix; -import android.os.Bundle; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewGroup; -import android.view.View.OnTouchListener; -import android.widget.ImageView; - -public class ImageActivity extends Activity implements OnTouchListener{ - - private ImageView image; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - RotatedImageView view = new RotatedImageView(this); - view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT)); - - setContentView(view); - -// setContentView(R.layout.activity_image); -// loadImage(); - } - - private void loadImage(){ - image = (ImageView) findViewById(R.id.backgroundimage); - image.setOnTouchListener(this); - } - - @Override - public boolean onTouch(View v, MotionEvent event) { - - double r = Math.atan2(event.getX() - image.getWidth() / 2, image.getHeight() / 2 - event.getY()); - int rotation = (int) Math.toDegrees(r); - - if (event.getAction() == MotionEvent.ACTION_MOVE) - { - updateRotation(rotation); - } - - return true; - } - - private void updateRotation(double rot) - { - float newRot = new Float(rot); - - Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan); - - Matrix matrix = new Matrix(); - matrix.postRotate(newRot - 50); - - Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); - image.setImageBitmap(redrawnBitmap); - } - - - -} diff --git a/CompassExample/src/com/novoda/example/compass/activities/CompassActivity.java b/CompassExample/src/com/novoda/example/compass/activities/CompassActivity.java index 5bbbfc1a..a294c96a 100644 --- a/CompassExample/src/com/novoda/example/compass/activities/CompassActivity.java +++ b/CompassExample/src/com/novoda/example/compass/activities/CompassActivity.java @@ -3,6 +3,7 @@ import com.actionbarsherlock.view.Menu; import com.novoda.example.compass.R; import com.novoda.example.compass.utils.CompassUtils; +import com.novoda.example.compass.view.RotatedImageView; import android.hardware.SensorEvent; import android.os.Bundle; @@ -12,11 +13,13 @@ public class CompassActivity extends BasicSensorActivity { private boolean isCompassEnabled; + private RotatedImageView image; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_compass); + image = (RotatedImageView) findViewById(R.id.image); isCompassEnabled = false; refreshText(); } @@ -26,13 +29,22 @@ private void refreshText() { + " Rotation in degress " + getRotation()) : "Compass disabled"; updateText(text); } + + private void refreshImageRotation() { + if (isCompassEnabled){ + image.updateCompassDegrees(getRotation()); + } + } @Override public void onSensorChanged(SensorEvent evt) { super.onSensorChanged(evt); refreshText(); + refreshImageRotation(); } + + private void updateText(String textString) { TextView text = (TextView) findViewById(android.R.id.text1); text.setText(textString); @@ -49,6 +61,7 @@ public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { switch (item.getItemId()) { case R.id.menu_enable_compass_rotation: isCompassEnabled = !isCompassEnabled; + image.setCompassActivated(isCompassEnabled, getRotation()); String text = isCompassEnabled ? "Compass rotation has been enabled!" : "Compass rotation has been disabled"; Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); diff --git a/CompassExample/src/com/novoda/example/compass/view/RotatedImageView.java b/CompassExample/src/com/novoda/example/compass/view/RotatedImageView.java index e11569c1..08217e02 100644 --- a/CompassExample/src/com/novoda/example/compass/view/RotatedImageView.java +++ b/CompassExample/src/com/novoda/example/compass/view/RotatedImageView.java @@ -8,16 +8,13 @@ import android.graphics.Canvas; import android.graphics.PointF; import android.graphics.drawable.Drawable; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; -public class RotatedImageView extends View implements SensorEventListener { +public class RotatedImageView extends View { private static final int INVALID_POINTER_ID = -1; @@ -341,17 +338,6 @@ public void setCompassActivated(boolean activated, float originalDegrees) { public void updateCompassDegrees(float degrees) { compass_degrees = -degrees; - } - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - // TODO Auto-generated method stub - - } - - @Override - public void onSensorChanged(SensorEvent event) { - // TODO Auto-generated method stub invalidate(); }