normalize For Orientation - Android User Interface

Android examples for User Interface:Window

Description

normalize For Orientation

Demo Code


//package com.java2s;

import android.graphics.Point;
import android.hardware.SensorEvent;
import android.view.Display;
import android.view.Surface;
import android.view.Window;

public class Main {
    public static void normalizeForOrientation(Window window,
            SensorEvent event) {//from  www. ja v a2 s .com
        Display display = window.getWindowManager().getDefaultDisplay();
        int rotation = display.getRotation();
        int height;
        int width;
        Point size = new Point();
        display.getSize(size);
        height = size.y;
        width = size.x;
        float x = 0;
        float y = 0;
        switch (rotation) {
        case Surface.ROTATION_90:
            if (width > height) {
                //landscape
                x = -event.values[1];
                y = event.values[0];
            } else {
                //reverse portrait
                x = -event.values[0];
                y = -event.values[1];
            }
            break;
        case Surface.ROTATION_180:
            if (height > width) {
                //reverse portrait
                x = -event.values[0];
                y = -event.values[1];
            } else {
                //reverse landscape
                x = event.values[1];
                y = -event.values[0];
            }
            break;
        case Surface.ROTATION_270:
            if (width > height) {
                //reverse landscape
                x = event.values[1];
                y = -event.values[0];
            } else {
                //portrait
                x = event.values[0];
                y = event.values[1];
            }
            break;
        default:
            if (height > width) {
                //portrait
                x = event.values[0];
                y = event.values[1];
            } else {
                //landscape
                x = -event.values[1];
                y = event.values[0];
            }
        }
        event.values[0] = x;
        event.values[1] = y;
    }
}

Related Tutorials