android – 使用ScrollView缩放缩放:当子大小更改时更新其滚动行为
作者:互联网
我有一个自定义类,我从ScrollView扩展.我想要做的是用这个类实现缩放缩放,我想用它来缩放到ScrollView的子节点并根据需要滚动缩放的内容.基本上,我想要iOS的UIScrollView类所具有的相同行为(它是一个SHAME,Android不提供任何内置的缩放功能,如iOS,我们必须从头开始编写所有内容).我在我的类中有一个ScaleGestureListener,当我收到onScale事件时,我更新了ScrollView内部子视图的布局维度:我将布局宽度和高度乘以我从ScaleGestureListener收到的比例因子.困扰我的是ScrollView不会根据其子项的新维度更新其滚动条.例如,如果子项的宽度大于ScrollView的宽度,则不会显示水平滚动条.我在滚动视图及其子节点上调用invalidate和requestLayout,但它们都失败了.
在下面我给出我的代码:
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根据孩子的更新大小更新其滚动条,每次用户进行缩放放大.
提前致谢
解决方法:
这是一个解决方案:
>我没有扩展ScrollView,而是编写了一个扩展FrameLayout的TwoDimensionScrollView,它是ScrollView和HorizontalScrollView的组合,因此它可以显示水平/垂直滚动条,即使缩放了子项也是如此.我也没有使用手势检测器.相反,我实现了自己的缩放缩放.
>要更新滚动条,请重写以下函数:
@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;
}
>您还需要找到所有调用SpringBack()和fling()的代码,然后使用上面的函数getScrollHorizontalMin()和getScrollVerticalMin()而不是0来设置x和y的最小值.
标签:android,android-layout,scrollview,pinchzoom 来源: https://codeday.me/bug/20190625/1284978.html