定向平板电脑与手机

时间:2012-07-10 16:44:43

标签: android android-orientation

所以我刚刚在三星平板电脑上测试了我在手机设备上测试过的应用程序。

应用程序根据加速器数据更新图形,我注意到在平板电脑上它将横向视为肖像..它在自然肖像模式设备上很好..

任何想法如何我可以调整它以便它同时适用于:

public class ARLaunch extends Activity {

/** Open Camera View **/    
private CamLayer camPreview;
/** Open Camera View **/ 
private GLLayer glView;

private WakeLock mWakeLock;

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

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    //Set Screen Orientation
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    try{
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");


        //Create Intance of Camera
        camPreview = new CamLayer(this.getApplicationContext());

        //Create Instance of OpenGL
        glView = new GLLayer(this);

        //FrameLayOut for holding everything
        FrameLayout frame = new FrameLayout(this);
        // set as main view
        setContentView(frame);

        // add Camera to view 
        frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        frame.addView(glView);



    } catch(Exception e){}
}
/** Remember to resume the glSurface  */
@Override
protected void onResume() {
    super.onResume();
    try{
    this.mWakeLock.acquire();
    } catch (Exception ex){}
    glView.onResume();
    glView.setZOrderOnTop(true);
}
/** Also pause the glSurface  */
@Override
protected void onPause() {
    super.onPause();
    try{
        this.mWakeLock.release();
    } catch (Exception ex){}

    glView.onPause();

}

public void displayOri(float acc, float ori){

}

}

public class PhoneOrientation {
private SensorManager sensorMan;
private Sensor sensorAcce;
private Sensor sensorMagn;
private SensorEventListener listener;
private float matrix[]=new float[16];
private Context ctx;

public PhoneOrientation(Context context) {
    ctx = context;
}

public void start(Context context) {
    listener = new SensorEventListener() {
        private float orientation[]=new float[3];
        private float acceleration[]=new float[3];

        public void onAccuracyChanged(Sensor arg0, int arg1){}

        public void onSensorChanged(SensorEvent evt) {
            int type=evt.sensor.getType();

            //Smoothing the sensor data a bit seems like a good idea.
            if (type == Sensor.TYPE_MAGNETIC_FIELD) {
                orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
                orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
                orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
            } else if (type == Sensor.TYPE_ACCELEROMETER) {
                acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
                acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
                acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
            }
            if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
                float newMat[]=new float[16];
                //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
                //toast.show();
                SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
                SensorManager.remapCoordinateSystem(newMat,
                        SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                        newMat);
                matrix=newMat;
            }
        }
    };

    sensorMan = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    sensorAcce = sensorMan.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    sensorMagn = sensorMan.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);

    sensorMan.registerListener(listener, sensorAcce, SensorManager.SENSOR_DELAY_GAME);
    sensorMan.registerListener(listener, sensorMagn, SensorManager.SENSOR_DELAY_GAME);      
}

public float[] getMatrix() {
    return matrix;
}

public void finish() {
    sensorMan.unregisterListener(listener);
}

}

1 个答案:

答案 0 :(得分:4)

阅读这篇文章:http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html

此外,请勿在活动可见时使用唤醒锁定以保持屏幕开启。改为在视图上使用KEEP_SCREEN_ON标志,您可以删除唤醒锁定权限。