ScrollView的捏合缩放:当子视图大小改变时更新其滚动行为

5
我有一个自定义类,它是从ScrollView扩展而来的。我想要做的是在这个类中实现捏合缩放功能,缩放到ScrollView的子级,并根据需要滚动缩放后的内容。基本上,我想要的行为与iOS的UIScrollView类相同(很遗憾Android没有像iOS那样提供内置缩放功能,我们必须从头开始编写所有内容)。我的类中有一个ScaleGestureListener,当我收到onScale事件时,我会更新ScrollView内部子视图的布局尺寸:用我从ScaleGestureListener接收到的比例因子乘以布局的宽度和高度。让我困扰的是,ScrollView不会根据其子级的新尺寸更新滚动条。例如,如果子级的宽度变大超过了ScrollView的宽度,它不会显示水平滚动条。我在滚动视图及其子级上都调用了invalidaterequestLayout,但它们都失败了。
以下是我的代码:
public class PinchZoomScrollView extends ScrollView{

private ScaleGestureDetector mScaleDetector;// = new ScaleGestureDetector(context, new ScaleListener());
private float mScaleFactor = 1.0f;
private float referenceTextSize;
private static final float MIN_ZOOM_AMOUNT = 1.0f;
private static final float MAX_ZOOM_AMOUNT = 2.0f;

private float refChildWidth;
private float refChildHeight;

private boolean shownForTheFirstTime = true;
//private View content;


private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    private WeakReference<PinchZoomScrollView> containerRef;

    public ScaleListener(PinchZoomScrollView container)
    {
        super();

        containerRef = new WeakReference<PinchZoomScrollView>(container);
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

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

        //Get Child object
        View content = containerRef.get().getChildAt(0);

        if(shownForTheFirstTime)
        {
            shownForTheFirstTime = false;

            refChildWidth  = content.getWidth();
            refChildHeight = content.getHeight();
        }

        LayoutParams params = new LayoutParams(Math.round(mScaleFactor*refChildWidth), Math.round(mScaleFactor*refChildHeight));
        content.setLayoutParams(params);


        System.out.println("this.width="+containerRef.get().getWidth());
        System.out.println("content.width="+Math.round(mScaleFactor*refChildWidth));


        content.invalidate();
        content.requestLayout();

        containerRef.get().updateViewLayout(content, params);

        containerRef.get().invalidate();
        containerRef.get().requestLayout();

        return true;
    }
}


public PinchZoomScrollView(Context context)
{
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

public PinchZoomScrollView(Context context, AttributeSet attrs)
{
    super(context,attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

public PinchZoomScrollView(Context context, AttributeSet attrs, int defStyle)
{
    super(context,attrs,defStyle);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    super.onTouchEvent(ev);

    mScaleDetector.onTouchEvent(ev);

    return true;
}

}

简而言之,每当用户进行缩放操作时,我需要ScrollView根据其子项的更新尺寸来更新滚动条。谢谢。
1个回答

4
这里是一个解决方案:
  1. Rather than extending ScrollView, I wrote a TwoDimensionScrollView extending FrameLayout which is a combination of ScrollView and HorizontalScrollView so that it can show both horizontal/vertical scrollbars, even if the child is scaled. Also I did not use gesture detector. Instead, I implement my own pinch-to-zoom.

  2. To update scroll bars, rewrite the functions below:

    @Override
    protected int computeVerticalScrollRange() {
        final int count = getChildCount();
        final int contentHeight = getHeight() - getPaddingBottom()
            - getPaddingTop();
        if (count == 0) {
            return contentHeight;
        }
    
    
    @Override
    protected int computeVerticalScrollOffset() {
        int result = Math.max(0, super.computeVerticalScrollOffset());
        return result - getScrollVerticalMin();
    }
    @Override
    protected int computeHorizontalScrollRange() {
        final int count = getChildCount();
        final int contentWidth = getWidth() - getPaddingLeft()
            - getPaddingRight();
        if (count == 0) {
            return contentWidth;
        }
    
        int scrollRange = getChildAt(0).getRight();
        final int scrollX = getScrollX();
        final int overscrollRight = Math.max(0, scrollRange - contentWidth);
        if (scrollX < 0) {
            scrollRange -= scrollX;
        } else if (scrollX > overscrollRight) {
            scrollRange += scrollX - overscrollRight;
        }
        return (int) (scrollRange * mScaleFactor + getScrollHorizontalMin());
    }
    
    @Override
    protected int computeHorizontalScrollOffset() {
        int result = Math.max(0, super.computeHorizontalScrollOffset());
        return result - getScrollHorizontalMin();
    }
    @Override
    protected int computeVerticalScrollExtent() {
        return getHeight() + getScrollVerticalMin();
    }
    
    @Override
    protected int computeHorizontalScrollExtent() {
        return getWidth() + getScrollHorizontalMin();
    }
    private int getScrollHorizontalMin() {
        if (this.getChildCount() < 1)
            return 0;
        View child = this.getChildAt(0);
        return (int) ((1 - mScaleFactor) * child.getPivotX());
    }
    
    private int getScrollVerticalMin() {
        if (this.getChildCount() < 1)
            return 0;
        View child = this.getChildAt(0);
        return (int) ((1 - mScaleFactor) * child.getPivotY());
    }
    private int getScrollVerticalRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = (int) Math.max(0, child.getHeight() * mScaleFactor
                    - (getHeight() - getPaddingBottom() - getPaddingTop())
                    + getScrollVerticalMin());
        }
    return scrollRange;
    }
    
    private int getScrollHorizontalRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = (int) Math.max(0, child.getWidth() * mScaleFactor
                    - (getWidth() - getPaddingRight() - getPaddingLeft())
                    + getScrollHorizontalMin());
        }
        return scrollRange;
    }
    
  3. You also need to find all code calling SpringBack() and fling(), and then set the min value for x and y using the above functions getScrollHorizontalMin() and getScrollVerticalMin() instead of 0


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接