0

我有一个变量“azimuth_angle”

我希望当我触摸屏幕并改变方向时

变量“azimuth_angle”将减少

在我的代码中

触摸屏幕变量可以减小

但改变它不能工作的方向

我使用 invalidate() 相同

这有什么问题?

public class HelloCamera extends Activity implements SensorEventListener
{
    private MyView mView;
    float azimuth_angle = 365;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        mView = new MyView(this);       

        setContentView(new HelloCameraPreview(this));

        addContentView(mView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

    private class MyView extends View 
    {
        private Paint mPaint;

        public MyView(Context context) 
        {
            super(context);
            mPaint = new Paint();
            mPaint.setColor(Color.YELLOW); 
            mPaint.setTextSize(12);
        }

        public void onDraw(Canvas canvas) 
        {
            float x = 10;
            float y = 20;
            canvas.drawText(String.valueOf(azimuth_angle), x, y, mPaint );
            azimuth_angle--;
        }

        public boolean onTouchEvent(MotionEvent event) 
        {
            invalidate();//But this can work
            return true;
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
         if(event.sensor.getType() == Sensor.TYPE_ORIENTATION)
         {
             azimuth_angle = event.values[0];
             mView.invalidate();//it doesnt work
         } 

    }
}

4

1 回答 1

0

由于传感器事件未在 UI 线程中传递给您,因此您需要使用 postInvalidate() 而不是 invalidate()。

于 2012-10-14T15:10:37.080 回答