3

我正在开发一个应用程序,我需要在其中根据设备倾斜信息进行文本滚动。我尝试了一些解决方案,但似乎没有任何效果。我可以使用以下代码获取倾斜信息:-

public class SensorActivity  extends Activity implements SensorEventListener {

    float[] mMagnetValues      = new float[3];
    float[] mAccelValues       = new float[3];
    float[] mOrientationValues = new float[3];
    float[] mRotationMatrix    = new float[9];

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
        TextView txt1 = (TextView) findViewById(R.id.textView1);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                System.arraycopy(event.values, 0, mAccelValues, 0, 3);
                break;

            case Sensor.TYPE_MAGNETIC_FIELD:
                System.arraycopy(event.values, 0, mMagnetValues, 0, 3);
                break;
        }
        SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelValues, mMagnetValues);
        SensorManager.getOrientation(mRotationMatrix, mOrientationValues);
        Log.e("Values", mOrientationValues[0] + " " + mOrientationValues[1] + " " + mOrientationValues);
    };

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) { }

    public void scrollTextUp() {
        ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
        scrollView.scrollBy(0, 5);
    }

    public void scrollTextBottom() {
        ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
        scrollView.scrollBy(0, -5);
    }
}

我认为mOrientationValues[1]需要使用它来滚动,但如何使用这个值对我来说是一个大问题。请帮忙。

任何帮助将非常感激。如果您发现任何好的教程或示例,请分享。

谢谢。

4

1 回答 1

2

所以在玩了几个小时之后,这就是我使用的解决方案。我的应用程序的要求是以可读的速度滚动文本,这就是我使用恒定滚动速度的原因,但我欢迎大家根据倾斜速度等来改进这个答案,这样它也可以帮助其他人。

TiltScrollController 类 -

public class TiltScrollController implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mAccelSensor;
    private Sensor mMagnetSensor;
    private float mCurrentPosition;
    private boolean isCurrentPositionSet = false;
    private int count = 0;
    private ScrollListener mListener;

    float[] mMagnetValues      = new float[3];
    float[] mAccelValues       = new float[3];
    float[] mOrientationValues = new float[3];
    float[] mRotationMatrix    = new float[9];

    public TiltScrollController(SensorManager sensorManager, ScrollListener listener) {
        mSensorManager = sensorManager;
        mListener = listener;
        mAccelSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mMagnetSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                System.arraycopy(event.values, 0, mAccelValues, 0, 3);
                break;

            case Sensor.TYPE_MAGNETIC_FIELD:
                System.arraycopy(event.values, 0, mMagnetValues, 0, 3);
                break;
        }
        SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelValues, mMagnetValues);
        SensorManager.getOrientation(mRotationMatrix, mOrientationValues);
        if (!isCurrentPositionSet) {
            mCurrentPosition = mOrientationValues[1];
            count += 1;
            if(count == 2) {
                isCurrentPositionSet = true;
            }
        }
        if(mCurrentPosition + 0.25 < mOrientationValues[1]) {
            //Scroll to Top
            mListener.onTiltUp();
        }
        else if (mCurrentPosition - 0.25 > mOrientationValues[1]) {
            //Scroll to Bottom
            mListener.onTiltDown();
        }
    };

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    public void requestAllSensors() {
        mSensorManager.registerListener(this, mAccelSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mMagnetSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void killAllSensors() {
        mSensorManager.unregisterListener(this, mAccelSensor);
        mSensorManager.unregisterListener(this, mMagnetSensor);
    }

    public interface ScrollListener {
        public void onTiltUp();
        public void onTiltDown();
    }

}

你的活动课——

public class SensorActivity  extends Activity {

    private ScrollView mScrollView;
    private TiltScrollController mTiltScrollController;
    private SensorManager mSensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mScrollView = (ScrollView)findViewById(R.id.scroll);
        mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        mTiltScrollController = new TiltScrollController(mSensorManager, new TiltScrollListener());
    }

    public void scrollTextUp() {
        mScrollView.smoothScrollBy(0, 6);
    }

    public void scrollTextBottom() {
        mScrollView.smoothScrollBy(0, -6);
    }


    @Override
    protected void onPause() {
        mTiltScrollController.killAllSensors();
        super.onPause();
    }

    public class TiltScrollListener implements TiltScrollController.ScrollListener {

        @Override
        public void onTiltUp() {
            scrollTextUp();
        }

        @Override
        public void onTiltDown() {
            scrollTextBottom();
        }
    }

}

干杯!!

于 2013-06-07T01:24:35.340 回答