在实际应用中,android2.3的浏览器中添加了横竖屏切换的平滑滑动的效果。
Browser的AndroidManifest.xml中
- <activity android:name="BrowserActivity"
- android:label="@string/application_name"
- android:launchMode="singleTask"
- android:alwaysRetainTaskState="true"
- android:configChanges="orientation|keyboardHidden"
- android:theme="@style/BrowserTheme"
- android:windowSoftInputMode="adjustResize" >
可以看到配置了android:configChanges="orientation|keyboardHidden"属性,而在BrowserActivity的onConfigurationChanged()方法中没发现有关于页面重绘的操作。而在framework/base/core/java/android/webkit文件夹中的webview中的
- protected void onConfigurationChanged (Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- int preRotateWidth;
- int preRotateHeight;
- Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- preRotateWidth = display.getWidth();
- preRotateHeight = display.getHeight();
- int orientation = display.getRotation();
- orientation = orientation * 90;
- if (orientation != mOrientation)
- {
- float angle = (float)(mOrientation - orientation);
- if (angle > 180.0f)
- {
- angle -= 360.0f;
- }
- else if (angle < -180.0f)
- {
- angle += 360.0f;
- }
- //smooth rotate
- RotateAnimation anim = new RotateAnimation(angle, TO_DEGREES,
- Animation.RELATIVE_TO_PARENT, PIVOT_X_VALUE, Animation.RELATIVE_TO_PARENT, PIVOT_Y_VALUE);
- OvershootInterpolator interp = new OvershootInterpolator(OVERSHOOT_TENSION);
- anim.setDuration(ANIMATION_DURATION);
- anim.setFillEnabled(true);
- anim.initialize(preRotateWidth,preRotateHeight,preRotateWidth,preRotateHeight);
- anim.setInterpolator(interp);
- anim.setAnimationListener(new SmoothRotateAnimationListener());
- ViewParent parent = getParent();
- // Find the parent view so that, if it is a View class (it can be ViewRoot class), then it's
- // preferable to start the animation on the parent because the parent View can cache its children
- // during animation. If Webview has no parent View, then the animation will be done by itself -
- // meaning that it will have to do the rotation and will be slow.
- if (parent instanceof View)
- ((View)parent).startAnimation(anim);
- else
- startAnimation(anim);
- mOrientation = orientation;
- }
- }
这个地方完成了平滑的实现。