下面列出了android.content.res.Configuration#ORIENTATION_PORTRAIT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
llStatus.setVisibility(View.GONE);
minuteChartView.setGridRows(6);
minuteChartView.setGridColumns(5);
minuteChartView.setGridChildRows(4);
viewLeftMessage.setVisibility(View.VISIBLE);
lineView.setVisibility(View.VISIBLE);
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {//竖屏
llStatus.setVisibility(View.VISIBLE);
minuteChartView.setGridRows(6);
minuteChartView.setGridColumns(5);
minuteChartView.setGridChildRows(4);;
viewLeftMessage.setVisibility(View.GONE);
lineView.setVisibility(View.GONE);
}
}
@Override
public void resetTranslations() {
parentFragment.toolbar.setBackgroundColor(Color.TRANSPARENT);
parentFragment.viewContainerBackground.setBackgroundColor(Color.TRANSPARENT);
parentFragment.descriptionText.setVisibility(View.GONE);
int orientation = Utils.getScreenOrientation(getActivity());
float density = getResources().getDisplayMetrics().density;
float tabsPadding = orientation == Configuration.ORIENTATION_PORTRAIT ? 0 : density * 48;
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) parentFragment.mTabs.getLayoutParams();
layoutParams.setMargins((int) tabsPadding, 0, (int) tabsPadding, 0);
parentFragment.mTabs.requestLayout();
setPaddingRecyclerview(orientation, density);
if (onScrollListener != null) onScrollListener.reset();
layoutManager.scrollToPositionWithOffset(0, 0);
}
private int getNumColumns(final int orientation) {
final int numColumns;
boolean singleColumnMultiWindow = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
singleColumnMultiWindow = Gallery.this.isInMultiWindowMode() && SettingValues.singleColumnMultiWindow;
}
if (orientation == Configuration.ORIENTATION_LANDSCAPE && SettingValues.isPro && !singleColumnMultiWindow) {
numColumns = Reddit.dpWidth;
} else if (orientation == Configuration.ORIENTATION_PORTRAIT
&& SettingValues.dualPortrait) {
numColumns = 2;
} else {
numColumns = 1;
}
return numColumns;
}
public SystemBarConfig(Activity activity) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mContentHeight = getContentHeight(activity);
mContentWidth = getContentWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
}
private boolean isFullSpan(int viewType) {
if (viewType == TYPE_HEADER) {
if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
return true;
} else return mImageStyle.getType() == Home.Style.Type.SQUARE ||
mImageStyle.getType() == Home.Style.Type.LANDSCAPE;
}
return false;
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
mLiveVideoBroadcaster.setDisplayOrientation();
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Resources res = getResources();
boolean isPortrait = res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (isPortrait) {
mColumnWidth = (int) (res.getDisplayMetrics().widthPixels * 0.87);
} else {
mColumnWidth = (int) (res.getDisplayMetrics().density * 320);
}
mGestureDetector = new GestureDetector(getContext(), new GestureListener());
mScroller = new Scroller(getContext(), new DecelerateInterpolator(1.1f));
mAutoScroller = new AutoScroller(getContext(), this);
mAutoScroller.setAutoScrollMode(snapToColumnWhenDragging() ? AutoScroller.AutoScrollMode.COLUMN : AutoScroller.AutoScrollMode
.POSITION);
mDragItem = new DragItem(getContext());
mDragColumn = new DragItem(getContext());
mDragColumn.setSnapToTouch(false);
mRootLayout = new FrameLayout(getContext());
mRootLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mColumnLayout = new LinearLayout(getContext());
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
mColumnLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mColumnLayout.setMotionEventSplittingEnabled(false);
mRootLayout.addView(mColumnLayout);
mRootLayout.addView(mDragItem.getDragItemView());
addView(mRootLayout);
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
public static int getDeviceDefaultOrientation(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Configuration config = context.getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
if (((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
config.orientation == Configuration.ORIENTATION_LANDSCAPE)
|| ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
return Configuration.ORIENTATION_LANDSCAPE;
} else {
return Configuration.ORIENTATION_PORTRAIT;
}
}
private int getScreenWidth() {
Resources resources = getContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration();
return configuration.orientation == Configuration.ORIENTATION_PORTRAIT ?
dm.widthPixels : dm.heightPixels;
}
/**
* 获取横屏屏幕宽度
*
* @param context
* @return
*/
public static int getVerticalScreenHeight(@NonNull Context context) {
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (context.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
return display.getHeight();
} else {
return display.getWidth();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
mLlStatus.setVisibility(View.GONE);
mKChartView.setGridRows(3);
mKChartView.setGridColumns(8);
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
mLlStatus.setVisibility(View.VISIBLE);
mKChartView.setGridRows(4);
mKChartView.setGridColumns(4);
}
}
@Override
public int getSpan() {
return Utils.getScreenOrientation(getActivity()) == Configuration.ORIENTATION_PORTRAIT ? 1 : 2;
}
public void setPaddingRecyclerview(int orientation, float density) {
float recyclerviewPadding = orientation == Configuration.ORIENTATION_PORTRAIT ? 0 : density * 48;
recyclerView.setPadding((int) recyclerviewPadding, recyclerView.getPaddingTop(), (int) recyclerviewPadding,
recyclerView.getPaddingBottom());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(LOG_TAG, "onConfigurationChanged() called");
final int newOrientation = newConfig.orientation;
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE ||
newOrientation == Configuration.ORIENTATION_PORTRAIT) {
// At this point, the screen has not be resized to match the new orientation.
// We use Handler.post so that we'll dispatch the ScreenOrientationChanged event after the
// screen has been resized to match the new orientation.
androidUIHandler.post(new Runnable() {
public void run() {
boolean dispatchEventNow = false;
if (frameLayout != null) {
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
if (frameLayout.getWidth() >= frameLayout.getHeight()) {
dispatchEventNow = true;
}
} else { // Portrait
if (frameLayout.getHeight() >= frameLayout.getWidth()) {
dispatchEventNow = true;
}
}
}
if (dispatchEventNow) {
recomputeLayout();
final FrameLayout savedLayout = frameLayout;
androidUIHandler.postDelayed(new Runnable() {
public void run() {
if (savedLayout != null) {
savedLayout.invalidate();
}
}
}, 100); // Redraw the whole screen in 1/10 second
// we do this to avoid screen artifacts left
// left by the Android runtime.
ScreenOrientationChanged();
} else {
// Try again later.
androidUIHandler.post(this);
}
}
});
}
}
public static boolean isPortrait(Context context) {
return (context.getResources()
.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
}
/**
* 返回当前屏幕是否为竖屏。
*
* @param context
* @return 当且仅当当前屏幕为竖屏时返回true, 否则返回false。
*/
public static boolean isScreenOriatationPortrait(@Nullable Context context) {
return context.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT;
}
/**
* Check if we are is in the portrait mode or not
*
* @return true if the we are currently in portrait mode
*/
private boolean isPortraitMode() {
int orientation = getContext().getResources().getConfiguration().orientation;
return orientation == Configuration.ORIENTATION_PORTRAIT;
}