Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return to default rotation after rotation #13

Closed
alashow opened this issue Dec 24, 2015 · 9 comments
Closed

Return to default rotation after rotation #13

alashow opened this issue Dec 24, 2015 · 9 comments

Comments

@alashow
Copy link
Contributor

alashow commented Dec 24, 2015

How can i return view to default rotation state with animation, after rotating with fingers? Now when i rotate view with fingers it stays in rotated state.

Like in the video:
https://youtu.be/Jh-OJjtHJmQ

@alexvasilkov
Copy link
Owner

There is no out of the box support for such feature, but I will consider adding it into the library.

For now you may try next code:

    gestureView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getActionMasked() == MotionEvent.ACTION_UP) {
                gestureView.onTouchEvent(event); // Ensure we passed event to gesture view

                State state = gestureView.getController().getState();
                float rotation = Math.round(state.getRotation() / 90f) * 90f;

                if (!State.equals(rotation, state.getRotation())) {
                    State end = new State();
                    end.set(state.getX(), state.getY(), state.getZoom(), rotation);

                    gestureView.getController().getStateController().restrictStateBounds(end);
                    gestureView.getController().animateStateTo(end);
                }
                return true;
            } else {
                return false;
            }
        }
    });

Note, that restrictStateBounds(end) is not a public method, so you have to call it with reflection.

@alashow
Copy link
Contributor Author

alashow commented Jan 11, 2016

Reflection didn't work before v2.1.0. Now works. Thanks 😃

State end = new State();
end.set(state.getX(), state.getY(), state.getZoom(), rotation);

StateController stateController = holder.image.getController().getStateController();
try {
    Method method = StateController.class.getMethod("restrictStateBounds", State.class);
    method.invoke(stateController, end);
} catch (Exception e) {
    e.printStackTrace();
}

@alashow alashow closed this as completed Jan 11, 2016
@alexvasilkov
Copy link
Owner

Hm, strange, this method was there almost from the beginning.
BTW, now you should not need to call restrictStateBounds(end) with reflection, state restrictions should be applied automatically inside animateStateTo(end).

@alashow
Copy link
Contributor Author

alashow commented Jan 11, 2016

My bad. I didn't see logs, NoSuchMethodException were throwing. Yeah, it works now because you updated animateStateTo(end), i think.

@alexvasilkov
Copy link
Owner

In new version 2.1.1 I added setRestrictRotation option in Settings which should do exactly what you want. Please check if works fine for you.

@alashow
Copy link
Contributor Author

alashow commented Jan 12, 2016

Yes, thanks a lot!

btw, when are you planning to make "Allow scroll up/down gestures to close full image mode.", from TODO?

@alexvasilkov
Copy link
Owner

I'm not sure, I think it may take several months. It is an interesting feature, but it's hard to find a nice solution and I don't have enough time right now.

@alashow
Copy link
Contributor Author

alashow commented Jan 12, 2016

@alexvasilkov https://github.com/DrKLO/Telegram/blob/master/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java telegram-s photoviewer is awesome. maybe you will find some solution from it. His code hard to understand, but it works.

@alexvasilkov
Copy link
Owner

Yeah, I looked it a bit, but it is a complete mess. Anyway I have different implementation which requires different logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants