我想在锁定的纵向方向模式中找到相机屏幕方向,我在片段类中使用相机并且我已将屏幕方向设置为肖像,但我面临的问题是,当我将相机从纵向转为横向它变化,我需要设置捕获按钮仅在相机处于纵向模式时可见.任何人都可以帮我在纵向模式下更改方向吗?以下是我的代码:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1] < 6.5 && event.values[1] > -6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation = 1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation = 0;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
if (orientation == 0) {
// capture button visisble
} else {
// invisible
}
Run Code Online (Sandbox Code Playgroud) [更新]
按照建议,我更改了所有父视图控制器以支持所有方向.我的app结构如下:AppDelegate> RootViewController> Videos> VideoDetails> MPMoviePlayerViewController.
如果我改变所有这些以支持所有方向,视频将在风景中播放.但支持所有方向并不是我想要的,并导致其他问题.还有其他工作或我可以做的其他事情吗?
谢谢
[/更新]
我有一个基于肖像的iPhone应用程序,它使用MPMoviePlayerViewController的自定义子类显示视频.当用户按下播放时,我创建了这个类的实例,并按模式呈现它,如下所示:
- (IBAction) playPressed:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:self.currentVideoModel.videoFileName ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// MovieViewController is just a simple subclass of MPMoviePlayerViewController
self.moviePlayerController = [[MovieViewController alloc] initWithContentURL:fileURL];
// full screen code.
[self.moviePlayerController.moviePlayer setScalingMode:MPMovieScalingModeFill];
[self.moviePlayerController.moviePlayer setFullscreen:TRUE];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayerController];
}
Run Code Online (Sandbox Code Playgroud)
问题是它在纵向上播放得很好但是当我将iPhone转为横向时,视频仍然以纵向而非横向播放:(应用中的所有视图控制器仅支持纵向.
我的MPMoviePlayerViewController子类只覆盖以下方法以允许方向更改,但它没有任何影响:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Run Code Online (Sandbox Code Playgroud)
我甚至尝试以编程方式旋转视频,但绝对没有运气,它始终保持纵向模式.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation …Run Code Online (Sandbox Code Playgroud) iphone movie mpmovieplayercontroller orientation screen-orientation
无论如何,您可以使应用程序在方向之后保留缓冲区,而无需手动处理配置更改.
场景基本上:
当方向改变时,应保留视频缓冲区直到最新点.
需要重新加载新的布局(因为portait和lanscape具有不同的布局),因此让app重新启动活动以重新加载新资源是我认为最好的方法.
视频组件位于片段中.
我尝试但不可接受的替代方案:
第一个解决方法是保存视频的位置,在创建视图后,使用seekTo转到最后一个点.但是,这将导致设备再次重新缓冲.
我尝试使用片段setRetainInstance(true)并确保在方向更改后媒体播放器不会重新初始化.在每个onCreateView之后,我只需将mediaplayer.setDisplay()设置为新的surfaceholder.不幸的是,这导致媒体播放器进入错误状态,这又需要重置媒体播放器.
对此有何建议/解决方法?
编辑:我看到stackoverflow中的某个地方,你可以更改媒体播放器表面持有人,而媒体播放器在android 4.0+中处于活动状态.但是,我正在为android 3.x开发一个应用程序.
好吧,我正在尝试添加锁定方向按钮,但是当我打电话时
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
屏幕变为普通人像.我究竟做错了什么?
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
orientation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (x > 5){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (x < -5){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else if (y > 5){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (y < -5){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
}
});
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个以纵向和横向模式显示书籍的应用程序.很明显,有1页以肖像方式显示,2页以横向方式显示.每个模式都工作得很好但是当我将方向从纵向更改为横向时,viewPager尝试从纵向模式获取2个页面,而不是尝试从横向模式获取2个双页...实际上FragmentStatePagerAdapter保留了2个片段以纵向创建并在为横向模式创建2个双页之前使用它们.如果我再次以纵向模式切换,FragmentStatePagerAdapter使用以前在横向模式下创建的2个片段,所以我再次看到1个双页而不是1个单页等.如果我继续切换方向,我得到一个OutOfMemoryError,因为FragmentStatePagerAdapter永远不会在方向改变时冲洗它的碎片.
这里有2个用例,以便于理解:
我向右滑动,我看到第5页和第6页确定
我以纵向模式启动viewPager
我向左滑动,我看到第2页和第3页确定
public class PlayerFragmentActivity extends FragmentActivity {
private Intent mIntent;
private ViewPager mPortraitPager;
private ViewPager mLandscapePager;
private Boolean mIsLandscape;
private String mKey;
private int mNbPages;
private int mNbDoublePages;
private PageFactory mPageFactory;
private DoublePageFactory mDoublePageFactory;
private PagerAdapter mPagerAdapter;
@Override
protected void onStop(){
super.onStop();
mPagerAdapter = null;
mDoublePageFactory = null;
mPageFactory = null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); …Run Code Online (Sandbox Code Playgroud)android fragment screen-orientation android-fragments android-viewpager
我有一个应用程序,使一些背景人员.当后台工作正在运行时,会显示圆圈,如果在此期间旋转设备,则活动将"重置",我想避免这种情况.
出于这个原因,我决定在此过程中禁用方向.我已经看到了这个问题的差异线程,但没有一个有效的解决方案,至少在我的情况下.
发布的解决方案是关于修复活动方向,但您必须处理以下事实:如果您使用以下情况,则不会返回REVERSE方向:
getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
对于PORTRAIT和REVERSE_PORTRAIT情况,上面的函数都返回SCREEN_ORIENTATION_PORTRAIT(至少在我的测试中).
所以最后我用Rotation值来处理它,所以我的代码"禁用旋转"是:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch(rotation) {
case Surface.ROTATION_180:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_0:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
Run Code Online (Sandbox Code Playgroud)
并再次允许方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Run Code Online (Sandbox Code Playgroud)
这在使用Android 4.1.2的设备中完美运行,但在使用Android 4.2.1的设备中,它无法按预期工作.
我想在活动实时循环中管理轮换应该是一个常见问题,但我无法找到合适的解决方案.可能是我正在寻找错误的方向,所以任何帮助都是非常受欢迎的.
提前谢谢,伊万.
android rotation screen-orientation android-orientation android-activity
我为iOS7开发了一个应用程序,现在尝试为iOS8更新它.问题我有以下几点:
应用程序屏幕方向可以旋转,在某些情况下,一些按钮会大幅移动.我有一些指向这些按钮的弹出窗口,所以如果在屏幕旋转时弹出窗口,按钮移动,我也需要弹出窗口.
在iOS7中我通过以下方式做到了这一点:当屏幕旋转时我更新了约束
- (void) updateViewConstraints
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
self.Button.constant = (CGFloat)10;
}
else
{
self.Button.constant = (CGFloat)5;
}
[super updateViewConstraints];
}
Run Code Online (Sandbox Code Playgroud)
我也移动了弹出窗口
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if(TempDisplayPopoverController == examplePopoverController)
{
[examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Run Code Online (Sandbox Code Playgroud)
我最初加载弹出窗口
- (void) LoadPopover{
examplePopover = [[examplep alloc] initWithNibName:@"exampleP" bundle:nil];
[examplePopover setDelegate:self];
examplePopoverController = [[UIPopoverController alloc] initWithContentViewController: examplePopover];
[examplePopoverController setDelegate:self];
examplePopoverController.popoverContentSize = examplePopover.view.frame.size;
TempDisplayPopoverController = examplePopoverController;
if ([examplePopoverController isPopoverVisible])
{
[examplePopoverController dismissPopoverAnimated:YES];
}
else
{
[examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; …Run Code Online (Sandbox Code Playgroud) 即使我可以更改屏幕方向以更新状态并重新渲染,ImageBackground 组件仍然不会更新其宽度。
我有以下代码:
<View
onLayout={event => this.mylayoutchange(event)}
style={{ flex: 1, backgroundColor: 'green' }}
>
<ImageBackground
style={{ flex: 1, width: this.state.width, height: this.state.height }}
imageStyle={{ resizeMode: 'repeat' }}
source={require('../assets/images/my_background.jpg')}
>
<View>
<Text>.... Other code here....</Text>
</View>
</ImageBackground>
</View>;
Run Code Online (Sandbox Code Playgroud)
当用户改变设备的方向时, mylayoutchange() 函数被调用。它正确更新状态。渲染函数将更新。宽度和高度已正确更改,如 中所示console.log()。但是,无论出于何种原因,<ImageBackground>都不会更新其正确的宽度和高度。
当屏幕旋转时,背景图像不再填满屏幕的整个尺寸,而是看到绿色背景颜色。
我究竟做错了什么?
我的环境:
"react": "16.13.1",
"react-native": "0.63.2",
Run Code Online (Sandbox Code Playgroud) 我实现了onConfigurationChnage(..)来读取方向配置chnage上的视图高度和宽度值
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Log.i(TAG, "onConfigurationChanged: "+newConfig.orientation+", "+height+", "+width + ", "+mTempeFrameLayout.getWidth());
}
Run Code Online (Sandbox Code Playgroud)
但它总是回复旧值,如果方向得到改变,它给出了最后的方向(可能)值.
使用displaymetrics的屏幕宽度/高度正确,但对于任何视图都不起作用.
任何人都有想法..如何在方向变化上得到正确的价值?
这是一个很受欢迎的问题,但我找不到任何适用于Swift 2的解决方案.
该应用程序仅限肖像.但在观看YouTube等全屏视频时,用户应该可以旋转到横向.
在Objective C上,这是最简单的解决方案,我使用了很长时间:
AppDelegate file:
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS7 = @"MPInlineVideoFullscreenViewController";
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS8 = @"AVFullScreenViewController";
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if ([[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS7)] ||
[[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS8)]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
Run Code Online (Sandbox Code Playgroud)
这允许在视频全屏时的所有方向.否则,仅限肖像.
但是我很难在Swift上完成这项工作.当全屏视频是Swift上的播放器时,是否可以使屏幕旋转?
android ×6
fragment ×1
ios ×1
iphone ×1
movie ×1
objective-c ×1
orientation ×1
react-native ×1
rotation ×1
swift ×1
swift2 ×1
uipopover ×1
view ×1