android - 倾斜滚动文本?

时间:2013-06-06 04:26:20

标签: android scroll textview accelerometer tilt

我正在开发一款应用,我需要根据设备倾斜信息进行文字滚动。我尝试了很少的解决方案,但似乎没有任何工作。我可以使用以下代码获取倾斜信息: -

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]需要用来滚动,但是如何使用这个值对我来说是个大问题。请帮忙。

任何帮助都会非常感激。如果您找到了任何好的教程或示例,请分享。

感谢。

1 个答案:

答案 0 :(得分: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();
        }
    }

}

干杯!!