在TouchImageView中缩放图像时禁用垂直Viewpager

时间:2014-12-07 10:08:26

标签: android android-viewpager touchimageview

我正在使用VerticalViewPagerTouchImageView进行图片放大/缩小。当我放大图像并尝试向下拖动图像(以查看图像的底部)时,视图寻呼机会滑动到第二个图像。任何帮助将是欣赏。我需要禁用viewpager我试图改变垂直视图寻呼机的canScroll功能,但它没有帮助。

以下是我修改的代码:

protected boolean canScroll(View v, boolean checkV, int dy, int x, int y) {
   if(v instanceof TouchImageView)
   {
       Log.d("Here","Here");
       if(((TouchImageView) v).getCurrentZoom()!=1)
       {
           Log.d("Here","HereAgain");

           return false;
       }
   }

   else  if (v instanceof ViewGroup) {
        final ViewGroup group = (ViewGroup) v;
        final int scrollX = v.getScrollX();
        final int scrollY = v.getScrollY();
        final int count = group.getChildCount();
        // Count backwards - let topmost views consume scroll distance first.
        for (int i = count - 1; i >= 0; i--) {
            // TODO: Add versioned support here for transformed views.
            // This will not work for transformed views in Honeycomb+
            final View child = group.getChildAt(i);
            if (y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
                    x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
                    canScroll(child, true, dy, x + scrollX - child.getLeft(),
                            y + scrollY - child.getTop())) {
                return true;
            }
        }

       return checkV && ViewCompat.canScrollVertically(v, -dy);
    }

   return false;
}

2 个答案:

答案 0 :(得分:2)

要解决此问题,请创建一个自定义VerticalViewPager类,该类会覆盖滚动事件(onTouchEventonInterceptTouchEvent),然后从truefalse返回取决于您是否要滚动,这将通过预定义的布尔变量实现:

import android.content.Context;
import android.view.MotionEvent;

public class CustomizongVVP extends VerticalViewPager {

    public boolean scroll = true;

    public CustomizongVVP(Context context) {
        super(context);
    }

    public CustomizongVVP(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return scroll && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return scroll && super.onInterceptTouchEvent(event);
    }

    public void setScroll(boolean scroll) {
        this.scroll = scroll;
    }

}

并定义:

CustomizongVVP custom_vvp = new CustomizongVVP(this);
custom_vvp.setScroll(true); // to allow scrolling
custom_vvp.setScroll(false); // to disable scrolling

因此,您可以在truefalse之间随时切换,以使用custom_vvp.setScroll()方法允许和停用滚动。

答案 1 :(得分:1)

另一种方法是将TouchImageView子类化并覆盖canScrollVertically方法,如下所示:

@Override
public boolean canScrollVertically(int direction) {
    matrix.getValues(m);
    float y = m[Matrix.MTRANS_Y];

    if (getImageHeight() < viewHeight) {
        return false;

    } else if (y >= -1 && direction < 0) {
        return false;

    } else if (Math.abs(y) + viewHeight + 1 >= getImageHeight() && direction > 0) {
        return false;
    }

    return true;
}

如果图像缩小,则滑动更改页面。如果图像被放大,那么他们可以平移图像而不更改页面,直到它们到达缩放图像的顶部或底部,此时下一次滑动将改变页面。

希望有用