2013-09-29 17 views
5

Tôi đang làm một ứng dụng trong Android. Các ứng dụng có một số dấu ngoặc kép được lưu trong cơ sở dữ liệu và tôi muốn trượt các dấu ngoặc kép trong màn hình bằng cách sử dụng các cảm biến của điện thoại. Vì vậy, tôi muốn rằng khi điện thoại di chuyển sang bên trái, tôi có thể thấy trong màn hình báo giá tiếp theo và khi tôi di chuyển điện thoại sang bên phải để xem trong màn hình báo trước. Bạn nghĩ loại điện thoại cảm biến nào tốt hơn? Tôi đang cố gắng sử dụng Type_orientation nhưng tôi có lỗi rằng: Hằng số này không được chấp nhận ở cấp API 8. Làm thế nào tôi có thể thay thế nó bằng SensorManager.getOrientation(). Hoặc bạn có nghĩ là có cảm biến nào tốt hơn để tôi sử dụng không?Cách sử dụng SensorManager.getOrientation() thay vì TYPE_ORIENTATION

package com.example.prova1; 

import java.util.HashMap; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 



public class SlideQuote extends Activity implements SensorEventListener 
{ 
//a TextView 
private TextView quote; 
private TextView author; 
//the Sensor Manager 
private SensorManager sManager; 
float x; 
int id; 
int total; 
String s1,s2; 
    Database quotedatabase = new Database(this); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.slide_quote); 

     //get the TextView from the layout file 
     quote = (TextView) findViewById(R.id.textView3); 
     author = (TextView) findViewById(R.id.textView4); 
id=1; 
     //get a hook to the sensor service 

     sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    if(sManager.getSensorList(Sensor.TYPE_ORIENTATION).size()!=0){ 
     Sensor s =sManager.getSensorList(Sensor.TYPE_ORIENTATION).get(o); 
     sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL); 
     s1= quotedatabase.getQuote(id); 
     s2= quotedatabase.getAuthor(id); 
     total=quotedatabase.getQuotesCount(); 


     quote.setText(s1); 
     author.setText(s2); 
    } 
    } 
    //when this Activity starts 
    @Override 
protected void onResume() 
{ 
    super.onResume(); 
    /*register the sensor listener to listen to the gyroscope sensor, use the 
    callbacks defined in this class, and gather the sensor information as quick 
    as possible*/ 
    sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL); 
} 

    //When this Activity isn't visible anymore 
@Override 
protected void onStop() 
{ 
    //unregister the sensor listener 
    sManager.unregisterListener(this); 
    super.onStop(); 
} 

@Override 
public void onAccuracyChanged(Sensor arg0, int arg1) 
{ 
    //Do nothing. 
} 

@Override 
public void onSensorChanged(SensorEvent event) 
{ 
    //if sensor is unreliable, return void 
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) 
    { 
    return; 
    } 
    //else it will output the Roll, Pitch and Yawn values 
    x=event.values[2]; 

    if(x>25){ 
    if(id==total) 
    {id=1; 
    } 
    else{ 

    id++; 
    } 
    s1= quotedatabase.getQuote(id); 
     s2= quotedatabase.getAuthor(id); 
     quote.setText(s1); 
     author.setText(s2); 

    } 
    if(x<-25){ 
    if(id==1) 
    {id=total;} 
    else{ 
    id--;} 

    s1= quotedatabase.getQuote(id); 
     s2= quotedatabase.getAuthor(id);  
     quote.setText(s1); 
     author.setText(s2); 
    } 
    } 
@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    sManager.unregisterListener(this); 
    Intent backIntent = new Intent(getApplication(), Quote.class); 
     finish(); 
     startActivity(backIntent); 

} 


} 

Trả lời

0
trang

Android Motion Sensor được đưa ra một số ví dụ để sử dụng các cảm biến:

Android Motion Sensor

Đối với con quay hồi chuyển, bạn có thể sử dụng đoạn mã sau:

// Create a constant to convert nanoseconds to seconds. 
private static final float NS2S = 1.0f/1000000000.0f; 
private final float[] deltaRotationVector = new float[4](); 
private float timestamp; 

public void onSensorChanged(SensorEvent event) { 
    // This timestep's delta rotation to be multiplied by the current rotation 
    // after computing it from the gyro sample data. 
    if (timestamp != 0) { 
    final float dT = (event.timestamp - timestamp) * NS2S; 
    // Axis of the rotation sample, not normalized yet. 
    float axisX = event.values[0]; 
    float axisY = event.values[1]; 
    float axisZ = event.values[2]; 

    // Calculate the angular speed of the sample 
    float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); 

    // Normalize the rotation vector if it's big enough to get the axis 
    // (that is, EPSILON should represent your maximum allowable margin of error) 
    if (omegaMagnitude > EPSILON) { 
     axisX /= omegaMagnitude; 
     axisY /= omegaMagnitude; 
     axisZ /= omegaMagnitude; 
    } 

    // Integrate around this axis with the angular speed by the timestep 
    // in order to get a delta rotation from this sample over the timestep 
    // We will convert this axis-angle representation of the delta rotation 
    // into a quaternion before turning it into the rotation matrix. 
    float thetaOverTwo = omegaMagnitude * dT/2.0f; 
    float sinThetaOverTwo = sin(thetaOverTwo); 
    float cosThetaOverTwo = cos(thetaOverTwo); 
    deltaRotationVector[0] = sinThetaOverTwo * axisX; 
    deltaRotationVector[1] = sinThetaOverTwo * axisY; 
    deltaRotationVector[2] = sinThetaOverTwo * axisZ; 
    deltaRotationVector[3] = cosThetaOverTwo; 
    } 
    timestamp = event.timestamp; 
    float[] deltaRotationMatrix = new float[9]; 
    SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector); 
    // User code should concatenate the delta rotation we computed with the current rotation 
    // in order to get the updated rotation. 
    // rotationCurrent = rotationCurrent * deltaRotationMatrix; 
    } 
} 
+0

như thế nào mà thay thế ** NGƯỜI nghe TYPE_ORIENTATION **? –

Các vấn đề liên quan