2024年5月23日

Android 螢幕旋轉方向API

 


import android.content.Context;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

public class ScreenOrientationHelper {

public static boolean isPortrait(Context context) {
int rotation = getRotation(context);
return rotation == Surface.ROTATION_0;
}

public static boolean isLandscape(Context context) {
int rotation = getRotation(context);
return rotation == Surface.ROTATION_90;
}

public static boolean isReversePortrait(Context context) {
int rotation = getRotation(context);
return rotation == Surface.ROTATION_180;
}

public static boolean isReverseLandscape(Context context) {
int rotation = getRotation(context);
return rotation == Surface.ROTATION_270;
}

private static int getRotation(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
return display.getRotation();
}
return Surface.ROTATION_0; // Default to portrait if unable to get rotation
}
}

沒有留言:

張貼留言