thanks for your reply.
I changed this part to
if (mValues != null) {
matrix.reset();
matrix.setRotate(-mValues[0]);
mPath.transform(matrix);
}
However, the transformation still goes infinitely. what should be the
correct method ? thanks!
On Sep 9, 6:08 am, Kostya Vasilyev <[email protected]> wrote:
> Matrix transformation methods are cumulative. If you wish to apply absolute
> transformation values, you need to reset the matrix to identity first.
>
> --
> Kostya Vasilyev --http://kmansoft.wordpress.com
>
> 08.09.2010 15:19 пользователь "dadada" <[email protected]> написал:
>
> 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 <[email protected]> wrote:
>
>
>
> > hi all,
>
> > i tried th...
--
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