Hi,

I don't see any reference to the compass sensor in your code, just a
few references to 'bearing' how is it set ?

Regards

On Jun 15, 3:39 am, Raghav Sood <[email protected]> wrote:
> Hi all,
>
> According to the book I am learning from the following code should
> create a sad looking, but working, compass. All I have managed to get
> is the sad lookout of it. The compass draws correctly but doesn't
> rotate. I am testing it on a real device with a accelerometer. The
> rest of the files in the project have nothing to do with the rotation.
> Everything happens in this class. Could anyone point me to my mistake?
>  Any help would be appreciated.
>
> package com.raghavsood.compass;
>
> import android.content.Context;
> import android.graphics.*;
> import android.view.*;
> import android.util.AttributeSet;
> import android.content.res.Resources;
>
> public class CompassView extends View {
>
>   private Paint markerPaint;
>   private Paint textPaint;
>   private Paint circlePaint;
>   private String northString;
>   private String eastString;
>   private String southString;
>   private String westString;
>   private int textHeight;
>
>   private float bearing;
>   public void setBearing(float _bearing) {
>     bearing = _bearing;
>   }
>   public float getBearing() {
>     return bearing;
>   }
>
>   public CompassView(Context context) {
>     super(context);
>     initCompassView();
>   }
>
>   public CompassView(Context context, AttributeSet attrs) {
>     super(context, attrs);
>     initCompassView();
>   }
>
>   public CompassView(Context context,
>                      AttributeSet ats,
>                      int defaultStyle) {
>     super(context, ats, defaultStyle);
>     initCompassView();
>   }
>
>   protected void initCompassView() {
>     setFocusable(true);
>
>     Resources r = this.getResources();
>
>     circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
>     circlePaint.setColor(r.getColor(R.color.background_color));
>     circlePaint.setStrokeWidth(1);
>     circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
>
>     northString = r.getString(R.string.cardinal_north);
>     eastString = r.getString(R.string.cardinal_east);
>     southString = r.getString(R.string.cardinal_south);
>     westString = r.getString(R.string.cardinal_west);
>
>     textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
>     textPaint.setColor(r.getColor(R.color.text_color));
>
>     textHeight = (int)textPaint.measureText("yY");
>
>     markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
>     markerPaint.setColor(r.getColor(R.color.marker_color));
>   }
>
>   @Override
>   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
>     // The compass is a circle that fills as much space as possible.
>     // Set the measured dimensions by figuring out the shortest boundary,
>     // height or width.
>     int measuredWidth = measure(widthMeasureSpec);
>     int measuredHeight = measure(heightMeasureSpec);
>
>     int d = Math.min(measuredWidth, measuredHeight);
>
>     setMeasuredDimension(d, d);
>   }
>
>   private int measure(int measureSpec) {
>     int result = 0;
>
>     // Decode the measurement specifications.
>     int specMode = MeasureSpec.getMode(measureSpec);
>     int specSize = MeasureSpec.getSize(measureSpec);
>
>     if (specMode == MeasureSpec.UNSPECIFIED) {
>       // Return a default size of 200 if no bounds are specified.
>       result = 200;
>     } else {
>       // As you want to fill the available space
>       // always return the full available bounds.
>       result = specSize;
>     }
>     return result;
>   }
>
>   @Override
>   protected void onDraw(Canvas canvas) {
>     int px = getMeasuredWidth() / 2;
>     int py = getMeasuredHeight() /2 ;
>
>     int radius = Math.min(px, py);
>     // Draw the background
>     canvas.drawCircle(px, py, radius, circlePaint);
>     // Rotate our perspective so that the ‘top’ is
>     // facing the current bearing.
>     canvas.save();
>     canvas.rotate(-bearing, px, py);
>     int textWidth = (int)textPaint.measureText("W");
>     int cardinalX = px-textWidth/2;
>     int cardinalY = py-radius+textHeight;
>
>     // Draw the marker every 15 degrees and text every 45.
>     for (int i = 0; i < 24; i++) {
>       // Draw a marker.
>       canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
>
>       canvas.save();
>       canvas.translate(0, textHeight);
>
>       // Draw the cardinal points
>       if (i % 6 == 0) {
>         String dirString = "";
>         switch (i) {
>           case(0)  : {
>                        dirString = northString;
>                        int arrowY = 2*textHeight;
>                        canvas.drawLine(px, arrowY, px-5, 3*textHeight,
>                                        markerPaint);
>                        canvas.drawLine(px, arrowY, px+5, 3*textHeight,
>                                        markerPaint);
>                        break;
>                      }
>           case(6)  : dirString = eastString; break;
>           case(12) : dirString = southString; break;
>           case(18) : dirString = westString; break;
>         }
>         canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
>       }
>
>       else if (i % 3 == 0) {
>         // Draw the text every alternate 45deg
>         String angle = String.valueOf(i*15);
>         float angleTextWidth = textPaint.measureText(angle);
>
>         int angleTextX = (int)(px-angleTextWidth/2);
>         int angleTextY = py-radius+textHeight;
>         canvas.drawText(angle, angleTextX, angleTextY, textPaint);
>       }
>       canvas.restore();
>
>       canvas.rotate(15, px, py);
>     }
>     canvas.restore();
>   }
>
> }
>
> Thanks
> --
> Raghav 
> Soodhttp://www.raghavsood.com/http://www.androidappcheck.com/http://www.telstop.tel/

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to