alexvasilkov / GestureViews

ImageView and FrameLayout with gestures control and position animation
Apache License 2.0
2.37k stars 384 forks source link

Pan & zoom to a child view. #162

Closed ln206 closed 3 years ago

ln206 commented 3 years ago

Let's say I'm using GestureFrameLayout and the layout contains 1 parent view which contains many child views. When I zoom out and tap a child view, how do I pan & zoom to that child view?

alexvasilkov commented 3 years ago

It involves a bit of positions computations to correctly calculate a proper pivot point for this kind of animation.

A quick & dirty code to zoom out to default zoom level with pivot point set to the center of the button can look like this:

            final State state = layout.getController().getState().copy();
            final Matrix matrix = new Matrix();
            state.get(matrix);

            final int[] locButton = new int[2];
            final int[] locLayout = new int[2];
            button.getLocationOnScreen(locButton);
            layout.getLocationOnScreen(locLayout);
            float x = locButton[0] - locLayout[0] + 0.5f * b.getWidth();
            float y = locButton[1] - locLayout[1] + 0.5f * b.getHeight();

            final float[] point = { x, y };
            matrix.mapPoints(point);
            x = point[0];
            y = point[1];

            state.zoomTo(1f, x, y);
            layout.getController().setPivot(x, y);
            layout.getController().animateStateTo(state);