Alex-Hyun
Alex-Hyun

Reputation: 85

Use zoom in at scrollview

I made xml that have scrollview.

And in the scrollview, there are many images. I want to zoom in those whole things at once.

What I have to do?

Upvotes: 1

Views: 1276

Answers (1)

mihirjoshi
mihirjoshi

Reputation: 12201

Use custom layout class -

    public CrosswordGridLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CrosswordGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CrosswordGridLayout(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    protected void dispatchDraw(Canvas canvas) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    public void scale(float scaleFactor, float pivotX, float pivotY) {
        mScaleFactor = scaleFactor;
        mPivotX = pivotX;
        mPivotY = pivotY;
        this.invalidate();
    }

    public void restore() {
        mScaleFactor = 1;
        this.invalidate();
    }

}

Make a gesture listner -

private class OnPinchListener extends SimpleOnScaleGestureListener {

    float startingSpan; 
    float endSpan;
    float startFocusX;
    float startFocusY;


    public boolean onScaleBegin(ScaleGestureDetector detector) {
        startingSpan = detector.getCurrentSpan();
        startFocusX = detector.getFocusX();
        startFocusY = detector.getFocusY();
        return true;
    }


    public boolean onScale(ScaleGestureDetector detector) {
        crosswordGridLayout.scale(detector.getCurrentSpan()/startingSpan, startFocusX, startFocusY);
        return true;
    }

    public void onScaleEnd(ScaleGestureDetector detector) {
        crosswordGridLayout.restore();
    }
}

Use this on your view for pinch zoom function.

Upvotes: 2

Related Questions