可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have an app with several 'normal' activities which can run on either landscape or portrait. They are designed for and mostly used on portrait.
This app has one single activity which uses the camera and is locked on landscape. I 'simulate' this activity is on portrait by rotating images and texts 90 degree, so it looks like the rest of activities.
On some device, such as Samsung Galaxy Tab 7 and Galaxy S3, a rotation animation is shown when going from a normal portrait activity to camera landscape activity and back. This is confusing for user because landscape activity simulates being on portrait.
Is there a way to remove this rotation animation? Ideally I'd like to change to default portrait to portrait animation, but just removing rotation animation would be enough.
I've tried
overridePendingTransition(0, 0);
an other variations of that method without success.
[ADDED]
Following suggestions by @igalarzab, @Georg and @Joe, I've done this (still with no luck):
- Added android:configChanges="orientation|screenSize" to Manifest
- Added onConfigurationChanged
- Created a dummy animation which does nothing and added overridePendingTransition(R.anim.nothing, R.anim.nothing);
I had these results:
- onConfigurationChanged is called only when rotating same Activity (Activity A on portrait -> Activity A on landscape). But it's not called when going from Activity A on portrait -> Activity B on landscape
- This prevented Activity from being restarted when rotating, but it did NOT removed rotation animation (tested on Galaxy S3, Galaxy Nexus, Galaxy Tab 7.0 and Galaxy Tab 10.1)
- overridePendingTransition(R.anim.nothing, R.anim.nothing); removed normal transitions (portrait->portrait and landscape->landscape) but it didn't removed rotation animation (portrait->landscape and vice versa).
[VIDEO]
I've uploaded a video that shows animation I want to disable. This happens when changing from camera activity (locked to landscape) to other activity while holding phone on portrait:
http://youtu.be/T79Q1P_5_Ck
回答1:
Sorry there is no way to control the rotation animation. This is done way outside of your app, deep in the window manager where it takes a screenshot of the current screen, resizes and rebuilds the UI behind it, and then runs a built-in animation to transition from the original screenshot to the new rebuilt UI. There is no way to modify this behavior when the screen rotation changes.
回答2:
This is the way how the stock camera app disables rotation animation:
private void setRotationAnimation() { int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE; Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); winParams.rotationAnimation = rotationAnimation; win.setAttributes(winParams); }
Note: According to API Reference and comment below, this only works:
- On API level 18 or above
- The
FLAG_FULLSCREEN
flag is set for WindowManager.LayoutParams
of the Activity - The Activity is not covered by another window (e.g. the Power Off popup triggered by long pressing the Power button)
回答3:
You can set in the AndroidManifest a property called android:configChanges
where you can decide which changes you want to manage in code. In this way, the rotation change animation will be disabled and you can handle it as you want in the method onConfigurationChanged
of your Activity
.
回答4:
I've put that in the mainActivity and it cancelled the rotation animation:
@Override public void setRequestedOrientation(int requestedOrientation) { super.setRequestedOrientation(requestedOrientation); int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT; Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); winParams.rotationAnimation = rotationAnimation; win.setAttributes(winParams); }
More details here.
回答5:
You might have tried this already, but just in case:
Try defining a "do nothing" animation and call overridePendingTransition()
with its id. Maybe something like:
Sorry, I didn't have a Galaxy device to test with :)
回答6:
You can set in the AndroidManifest a property called android:configChanges where you can decide which changes you want to manage in code. In this way, the rotation change animation will be disabled and you can handle it as you want in the method onConfigurationChanged of your Activity.
This should work, but according to this page you should also add screenSize to configChanges by adding android:configChanges="orientation|screenSize" to your Activity Tag.
回答7:
If you want to set your activities in portrait mode only you can do it in your manifest file like this
回答8:
Against them who said no I say yes it is possible and it is very simple! The first thing may sound stupid but lock your application to the desired orientation! Then keep asking the gyrometer what orientation the device has an last but not least rotate or animate your views to the new orientation!
Edit: you may want to hide the system ui since it won't rotate. Mario