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