android compass with a random rotation(0~360)
<p>I'm making a compass that points to a user defined location.I'm using the ratation bmp to rotate the needle.The problem is that if the compass given degress near 0 degress it shows a random rotation.For example: if the compass given degrees 358, 359, 360,2,5...., the view give a random rotation.It rotation from 0 to 2, 5 while does not rotation from 358~2~5. All code below:</p>

<pre><code>public class DigitalCompass  extends View{
private float direction = 0;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private boolean firstDraw;

public DigitalCompass(Context context) {
    super(context);
    init();
}

public DigitalCompass(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DigitalCompass(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init(){

firstDraw = true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
    int cxCompass = getMeasuredWidth()/2;
    int cyCompass = getMeasuredHeight()/2;
    float radiusCompass;

if(cxCompass &gt; cyCompass){
    radiusCompass = (float) (cyCompass * 0.9);
}
else{
    radiusCompass = (float) (cxCompass * 0.9);
}
canvas.drawCircle(cxCompass, cyCompass, radiusCompass, paint);

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.compass);  

if(!firstDraw){

 float rotationAngle = 360f - direction;
 Matrix rotation = new Matrix();
 rotation.setRotate(rotationAngle, bmp.getWidth()/2.0f, bmp.getHeight()/2.0f);
 canvas.drawBitmap(bmp, rotation, null);

 //canvas.drawText(String.valueOf(direction), cxCompass, cyCompass, paint);
}

}

public void updateDirection(float dir)
{
    firstDraw = false;
    direction = dir;

    //Log.i(Utils.Tag, String.valueOf(direction));

    invalidate();
}
</code></pre>

<p>}</p>
