런타임 중에 방향을 잠그는 방법
런타임 중에 방향을 잠그는 방법이 있습니까? 예를 들어 사용자가 현재 가로 모드에있는 경우 사용자가 화면을 가로로 잠그고 메뉴 옵션을 토글 할 수 있도록하고 싶습니다.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
활동에서 호출되면 가로로 고정됩니다. ActivityInfo 클래스에서 다른 플래그를 찾으십시오. 다시 세로로 잠 그거나 센서 / 슬라이더 구동으로 만들 수 있습니다.
자세한 정보 : http://www.devx.com/wireless/Article/40792
getConfiguration이 반환하는 것과 setRequestedOrientation이 원하는 것의 차이에주의하십시오. 둘 다 int이지만 다른 상수 정의에서 비롯됩니다.
180도 뒤집기를 허용하면서 현재 방향을 잠그는 방법은 다음과 같습니다.
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
이것은 역 세로 및 역 가로 장치에서 작동합니다.
잠금 방향 :
int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation);
잠금 해제 방향 :
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
비슷한 경우가있는 것 같았습니다. 모든 오리엔테이션을 지원하고 싶었지만 워크 플로의 특정 지점이 지나면 현재 오리엔테이션을 유지해야했습니다. 내 솔루션은 다음과 같습니다.
보호 된 워크 플로 시작시 :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
보호 된 워크 플로 종료시 :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
태블릿에 대한 지원 @pstoppani 대답 대안 (@pstoppani 대답과 마찬가지로, 장치에서이만이 작업> 2.2)
에 -Tested Samsung Galaxy SIII
및Samsung Galaxy Tab 10.1
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}
내 코드는 다음과 같습니다. 이러한 방법 중 하나로 화면을 잠그고 작업이 완료되면 unlockOrientation으로 잠금을 해제 할 수 있습니다.
/** Static methods related to device orientation. */
public class OrientationUtils {
private OrientationUtils() {}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/** Locks the device window in actual screen mode. */
public static void lockOrientation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
// Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
// Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
if (!BuildVersionUtils.hasGingerbread()) {
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){
if (orientation == Configuration.ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
/** Unlocks the device window in user defined screen mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
위의 @pstoppani 답변의 Xamarin 변환은 다음과 같습니다.
참고 : 이것은 조각에 대한 것이므로 활동을 바꿉니다. 와 이. 활동 내에서 사용되는 경우.
public void LockRotation()
{
ScreenOrientation orientation;
var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation;
switch (surfaceOrientation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReversePortrait;
break;
default:
orientation = ScreenOrientation.ReverseLandscape;
break;
}
Activity.RequestedOrientation = orientation;
}
public void UnlockRotation()
{
Activity.RequestedOrientation = ScreenOrientation.Unspecified;
}
이것은 사용하기 전에 다른 접근 방식으로 진행되었으므로 테스트되지 않았지만 유용 할 수 있습니다.
참고URL : https://stackoverflow.com/questions/2366706/how-to-lock-orientation-during-runtime
'program story' 카테고리의 다른 글
JodaTime으로 특정 달의 마지막 날짜를 얻는 방법은 무엇입니까? (0) | 2020.08.09 |
---|---|
asp.net MVC 4 및 MVC 5에서 기본 컨트롤러를 설정하는 방법 (0) | 2020.08.09 |
이미 컴파일 된 APK에 서명하는 방법 (0) | 2020.08.08 |
JQuery를 사용하여 추가 할 HTML 템플릿 정의 (0) | 2020.08.08 |
텍스트 영역의 문자 계산 (0) | 2020.08.08 |