-
Notifications
You must be signed in to change notification settings - Fork 1.2k
05. Orientation changes
davemorrissey edited this page Mar 23, 2015
·
1 revision
When the screen orientation is changed, the view is loaded from scratch and all its state is forgotten, so the scale will be reset to the minimum (zoomed out), the image centered and the orientation set to zero (or the value you set in code).
If you want the current scale, center and orientation to be preserved when the screen is rotated, you can request it from the view's getState
method, and restore it after rotation, by passing it to the view along with the image source. Here's a simple example of how you might do this in a fragment.
private static final String BUNDLE_STATE = "ImageViewState";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
ImageViewState imageViewState = null;
if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_STATE)) {
imageViewState = (ImageViewState)savedInstanceState.getSerializable(BUNDLE_STATE);
}
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
imageView.setImage(ImageSource.asset("map.png"), imageViewState);
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
View rootView = getView();
if (rootView != null) {
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
ImageViewState state = imageView.getState();
if (state != null) {
outState.putSerializable(BUNDLE_STATE, imageView.getState());
}
}
}