hello all, i used matrix to rotate instead of the canvas.
However, the result isn't what i expected. The path just rotate infinitely without stopping. below is my code. it is a modification of the compass.java in apidemo. can someone please give me some advice on this? import android.content.Context; import android.graphics.*; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Config; import android.util.Log; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; public class Compass extends GraphicsActivity { private static final String TAG = "Compass"; private SensorManager mSensorManager; private SampleView mView; private float[] mValues; private final SensorListener mListener = new SensorListener() { public void onSensorChanged(int sensor, float[] values) { if (Config.LOGD) Log.d(TAG, "sensorChanged (" + values[0] + ", " + values[1] + ", " + values[2] + ")"); mValues = values; if (mView != null) { mView.invalidate(); } } public void onAccuracyChanged(int sensor, int accuracy) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); mView = new SampleView(this); setContentView(mView); } @Override protected void onResume() { if (Config.LOGD) Log.d(TAG, "onResume"); super.onResume(); mSensorManager.registerListener(mListener, SensorManager.SENSOR_ORIENTATION, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { if (Config.LOGD) Log.d(TAG, "onStop"); mSensorManager.unregisterListener(mListener); super.onStop(); } private class SampleView extends View { private Paint mPaint = new Paint(); private Path mPath = new Path(); private boolean mAnimate; private long mNextTime; private Matrix matrix = new Matrix(); public SampleView(Context context) { super(context); // Construct a wedge-shaped path mPath.moveTo(0, -50); mPath.lineTo(-20, 60); mPath.lineTo(0, 50); mPath.lineTo(20, 60); mPath.close(); matrix.postScale(10, 10); } @Override protected void onDraw(Canvas canvas) { Paint paint2 = new Paint(); paint2.setColor(Color.BLUE); paint2.setStyle(Paint.Style.FILL); paint2.setAlpha(120); Paint paint3 = new Paint(); paint3.setColor(Color.GREEN); paint3.setStyle(Paint.Style.FILL); paint3.setAlpha(200); Paint paint4 = new Paint(); paint4.setColor(Color.GRAY); paint4.setStyle(Paint.Style.FILL); paint4.setAlpha(120); Paint paint5 = new Paint(); paint5.setColor(Color.GRAY); paint5.setStyle(Paint.Style.FILL_AND_STROKE); paint5.setAlpha(120); Paint paint = mPaint; canvas.drawColor(Color.WHITE); paint.setAntiAlias(true); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); paint.setAlpha(120); int w = canvas.getWidth(); int h = canvas.getHeight(); int cx = w / 2; int cy = h / 2; canvas.translate(cx, cy); if (mValues != null) { matrix.setRotate(-mValues[0]); mPath.transform(matrix); // canvas.rotate(-mValues[0]); } canvas.drawPath(mPath, mPaint); String str = ""; String direction = ""; if (mValues != null) { str = "Azimuth: " + Float.toString(-mValues[0]); if(-mValues[0] < 0 && -mValues[0] > -1){ direction = "Direction: North"; }else if(-mValues[0] < 1 && -mValues[0] > -90){ direction = "Direction: North East"; }else if(-mValues[0] < -90 && -mValues[0] > -91){ direction = "Direction: East"; }else if(-mValues[0] < -91 && -mValues[0] > -180){ direction = "Direction: South East"; }else if(-mValues[0] < -180 && -mValues[0] > -181){ direction = "Direction: South"; }else if(-mValues[0] < -181 && -mValues[0] > -270){ direction = "Direction: South West"; }else if(-mValues[0] < -270 && -mValues[0] > -271){ direction = "Direction: West"; }else if(-mValues[0] < -271 && -mValues[0] > -360){ direction = "Direction: North West"; } } canvas.drawText(str, 0, 100, mPaint); canvas.drawText(direction, 0, 110, mPaint); canvas.drawCircle(0, 200, 20, paint2); canvas.drawCircle(0, 200, 4, paint3); canvas.drawCircle(0, 200, 30, paint4); canvas.drawCircle(0, 200, 50, paint4); } @Override protected void onAttachedToWindow() { mAnimate = true; super.onAttachedToWindow(); } @Override protected void onDetachedFromWindow() { mAnimate = false; super.onDetachedFromWindow(); } } } On Sep 8, 4:18 pm, dadada <ytbr...@gmail.com> wrote: > hi all, > > i tried the sample compass app in apidemo. When there's a new reading > in azimuth, i write the reading on canvas. However, the canvas is set > to rotate when new reading comes in, to simulate the rotation of the > compass arrow. > > Is there an alternative to just rotate the bitmap rather than the > canvas. there don seems to have rotate method on bitmap or path. > > any advices? > > /bryan -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en