Camera Invalidate()无法在onSensorChanged方法中使用

时间:2012-08-31 08:05:08

标签: android android-camera android-sensors

我有一个变量“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
         } 

    }
}

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 } } }

1 个答案:

答案 0 :(得分:0)

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