Permalink
Browse files

-using a rotatable imageview till the multi otuch view is ready

  • Loading branch information...
1 parent d292dc9 commit e4ed0723abdc4b599d3e6156ccda963bea521c2c @akshaydashrath akshaydashrath committed Jul 13, 2012
View
2 CompassExample/res/layout/activity_compass.xml
@@ -6,7 +6,7 @@
android:layout_height="match_parent" android:id="@android:id/text1"
android:padding="@dimen/padding_medium" android:layout_weight="10" />
- <com.novoda.example.compass.view.RotatedImageView
+ <com.novoda.example.compass.view.RotatableImageView
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:id="@+id/image" />
View
9 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.RotatableImageView;
import com.novoda.example.compass.view.RotatedImageView;
import android.hardware.SensorEvent;
@@ -13,13 +14,13 @@
public class CompassActivity extends BasicSensorActivity {
private boolean isCompassEnabled;
- private RotatedImageView image;
+ private RotatableImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
- image = (RotatedImageView) findViewById(R.id.image);
+ image = (RotatableImageView) findViewById(R.id.image);
isCompassEnabled = false;
refreshText();
}
@@ -32,7 +33,7 @@ private void refreshText() {
private void refreshImageRotation() {
if (isCompassEnabled){
- image.updateCompassDegrees(getRotation());
+ image.setRotationInDegrees(getRotation());
}
}
@@ -61,7 +62,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());
+ image.useCompassToRotate(isCompassEnabled);
String text = isCompassEnabled ? "Compass rotation has been enabled!"
: "Compass rotation has been disabled";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
View
113 CompassExample/src/com/novoda/example/compass/view/RotatableImageView.java
@@ -0,0 +1,113 @@
+package com.novoda.example.compass.view;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.novoda.example.compass.R;
+
+
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.view.View;
+
+/**
+ * This class extends the View class and is designed draw the compass on the
+ * View.
+ *
+ * @author Justin Wetherell <phishman3579@gmail.com>
+ */
+public class RotatableImageView extends View {
+ private static final AtomicBoolean drawing = new AtomicBoolean(false);
+ private static final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
+ private static int parentWidth = 0;
+ private static int parentHeight = 0;
+ private static Matrix matrix = null;
+ private static Bitmap bitmap = null;
+ private int rotationValue;
+ private boolean isCompassEnabled;
+
+ public RotatableImageView(Context context) {
+ super(context);
+
+ initialize();
+ }
+
+ public RotatableImageView(Context context, AttributeSet attr) {
+ super(context, attr);
+
+ initialize();
+ }
+
+ private void initialize() {
+ matrix = new Matrix();
+ bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+
+ parentWidth = MeasureSpec.getSize(widthMeasureSpec);
+ parentHeight = MeasureSpec.getSize(heightMeasureSpec);
+ setMeasuredDimension(parentWidth, parentHeight);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void onDraw(Canvas canvas) {
+ if (canvas == null)
+ throw new NullPointerException();
+
+ if (!drawing.compareAndSet(false, true))
+ return;
+
+ int bitmapWidth = bitmap.getWidth();
+ int bitmapHeight = bitmap.getHeight();
+
+ int canvasWidth = canvas.getWidth();
+ int canvasHeight = canvas.getHeight();
+ if (bitmap.getWidth() > canvasWidth || bitmap.getHeight() > canvasHeight) {
+ bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmapWidth * .9), (int) (bitmapHeight * .9), true);
+ }
+
+ int bitmapX = bitmap.getWidth() / 2;
+ int bitmapY = bitmap.getHeight() / 2;
+
+ int parentX = parentWidth / 2;
+ int parentY = parentHeight / 2;
+
+ int centerX = parentX - bitmapX;
+ int centerY = parentY - bitmapY;
+
+
+
+ if (isCompassEnabled){
+ matrix.setRotate(rotationValue, bitmapX, bitmapY);
+ }
+ matrix.postTranslate(centerX, centerY);
+
+ canvas.drawBitmap(bitmap, matrix, paint);
+
+ drawing.set(false);
+ }
+
+ public void setRotationInDegrees(int rotation){
+ this.rotationValue = rotation;
+ invalidate();
+ }
+
+ public void useCompassToRotate(boolean isCompassEnabled){
+ this.isCompassEnabled = isCompassEnabled;
+ }
+}

0 comments on commit e4ed072

Please sign in to comment.