Question

I have to be missing something obvious, since everyone on the planet seems to be able to get the whole pinch-zoom thing to work. The main difference is most folks seem to be zooming on images, whereas I've a GridLayout (android-support-v7-gridlayout) with a bunch of text views. The goal seems simple enough: Allow the user to zoom in so that they can view a portion of the full grid at a larger size.

The grid draws fine, and horizontal & vertical scrolling work fine. And then, when I do a two-finger gesture with fingers moving apart (zoom), nothing happens, despite getting the log message that onScale has been called. When I move the two fingers in (pinch), all the views disappear (though my scroll bars are left).

I've looked at Matrix transformations but haven't seen that those work on anything but an image view.

The basic layout xml:

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:id="@+id/chooser_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 </HorizontalScrollView>
</ScrollView>

Then in the activity

private ZoomGridLayout playerGrid;

and in onCreate:

    ViewGroup gridParent = (ViewGroup) findViewById(R.id.chooser_layout);
    playerGrid = new ZoomGridLayout(getBaseContext());
    playerGrid.setRowCount(8);
    playerGrid.setColumnCount(8);
    LayoutParams layout = 
            new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                            ViewGroup.LayoutParams.WRAP_CONTENT);
    playerGrid.setDetector();
    gridParent.addView(playerGrid, layout);

I then proceed to add ~150 small text views to the grid. Here's the first take at ZoomGridLayout:

public class ZoomGridLayout extends GridLayout {

    private final static String TAG = "ZGL";

    private ScaleGestureDetector scaleDetector;
    private Context context;
    public void setDetector() {
        scaleDetector = new ScaleGestureDetector(context, new ScaleGesture());
    }
    public ZoomGridLayout(Context c) {
        super(c);
        context = c;
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {
        scaleDetector.onTouchEvent(event);
        return true;
    }

    private class ScaleGesture
    extends ScaleGestureDetector.SimpleOnScaleGestureListener
    {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Log.v(TAG, detector.toString());
            ScaleAnimation scale = 
                    new ScaleAnimation(detector.getPreviousSpan(), 
                    detector.getCurrentSpan(), 
                     detector.getPreviousSpan(), 
                    detector.getCurrentSpan());
            scale.setFillAfter(true);
            startAnimation(scale);
            return true;
        }
    }
}

I've been beating my head against this for a couple days now. I'm new to Android programming, so not very familiar with the libraries, but I'm an experienced developer. Any pointers would be wonderful. TIA.

Was it helpful?

Solution

I gave up on animations (though eventually I'd like to try to make that work). Here's the new onScale method:

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Log.v(TAG, detector.toString());
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(MIN_SCALE, Math.min(mScaleFactor, MAX_SCALE));

            invalidate();

            return true;
        }

this is then complemented by overriding the draw method:

    @Override
    public void dispatchDraw(Canvas canvas) {
        String disp = String.valueOf(mScaleFactor);
            Log.v(TAG, "dispatch draw " + disp);

        canvas.save();
        canvas.scale(mScaleFactor, mScaleFactor);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

i also learned that since the parent is a ViewGroup instead of a View, that one needs to override the dispatch… methods instead of the on… methods.

there are so many different variants of this pattern out there. i hope this particular wrinkle helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top