如果ImageViewTouch处于缩放模式,请禁用PagerAdapter中的滑动

时间:2013-10-24 09:38:43

标签: android image-gallery

:d

我使用ImageViewTouch实现了一个图库:https://github.com/sephiroth74/ImageViewZoom 这样可以使用DoupleTap和其他漂亮的东西进行缩放,效果很好。

这些图像会添加到PagerAdapter中以启用图像之间的滑动。

但是,当图像放大时,我目前在x轴上滚动时遇到问题。 如果用户缩放了图像,我想禁用PageAdapters触摸侦听器。

但是,如果没有缩放,或者如果图像放大并且位于图像的远端,我想再打开它。

扩展FragmentActivity的GalleryActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_layout);

    GalleryAdapter galleryAdapter = new GalleryAdapter(GalleryActivity.this, images);

    // create view pager using the fragment adapter
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(galleryAdapter);
    viewPager.setOnPageChangeListener(new PageChangeListener());
}

我的适配器

public class GalleryAdapter extends PagerAdapter {

Matrix imageMatrix;
private Activity activity;
private ArrayList<Bitmap> images;
private LayoutInflater inflater;

// constructor
public GalleryAdapter(Activity activity, ArrayList<Bitmap> images) {
    this.activity = activity;
    this.images = images;
}

@Override
public int getCount() {
    return this.images.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    final ImageViewTouch imgDisplay;
    final Bitmap image;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View viewLayout = inflater.inflate(R.layout.gallery_image,
            container, false);

    imgDisplay = (ImageViewTouch) viewLayout.findViewById(R.id.image);
    imgDisplay.setDisplayType(DisplayType.FIT_IF_BIGGER);
    imgDisplay.setVisibility(View.GONE);
    image = images.get(position);

    imgDisplay.setDoubleTapEnabled(true);

    if (null == GalleryAdapter.this.imageMatrix) {
        GalleryAdapter.this.imageMatrix = new Matrix();
    } else {
        // get the current image matrix, if we want restore the
        // previous matrix once the bitmap is changed
        imageMatrix = imgDisplay.getDisplayMatrix();
    }

    imgDisplay.setImageBitmap(image, GalleryAdapter.this.imageMatrix
            .isIdentity() ? null : GalleryAdapter.this.imageMatrix,
            ImageViewTouchBase.ZOOM_INVALID,
            ImageViewTouchBase.ZOOM_INVALID);
    imgDisplay.setVisibility(View.VISIBLE);

    viewLayout.setTag(imgDisplay);

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);
}

这是否有人取得了成就?

祝你好运,  Henric

2 个答案:

答案 0 :(得分:7)

您应该使用自己的ImageView子类扩展ImageViewZoom。然后尝试这样的事情:

this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getScale() > 1f) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
});

每当图像缩放/缩放超过1x时,这应该基本上禁止分页。因此,用户必须缩小才能翻页。

请参阅此处https://github.com/sephiroth74/ImageViewZoom/issues/38

答案 1 :(得分:1)

我已经扩展了图像视图触摸并在下面发布了我的代码。此代码与Android图库更相似:双击总是缩小,除非您已缩小;如果放大,除非您处于图像边缘,否则将禁用滚动到下一个图像。希望有人觉得这很有用。 (我扩展了现有的类并进行了修改,直到它起作用,它可能不是最干净的代码:))

public class ExtendedImageViewTouch extends ImageViewTouch {

static final float SCROLL_DELTA_THRESHOLD = 1.0f;

public ExtendedImageViewTouch(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    init();
}

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

public ExtendedImageViewTouch(Context context) {
    super(context);
    init();
}

private void init() {
    OnTouchListener listener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (getScale() > 1f) {
                getParent().requestDisallowInterceptTouchEvent(true);
            } else {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            return false;
        }
    };
    setOnTouchListener(listener);
    setDisplayType(DisplayType.FIT_TO_SCREEN);
}

@Override
protected float onDoubleTapPost(float scale, float maxZoom) {
    if (scale!=1f) {
        mDoubleTapDirection = 1;
        return 1f;
    }
    if (mDoubleTapDirection == 1) {
        mDoubleTapDirection = -1;
        if ( ( scale + ( mScaleFactor * 2 ) ) <= maxZoom ) {
            return scale + mScaleFactor;
        } else {
                mDoubleTapDirection = -1;
                return maxZoom;
        }
    } else {
        mDoubleTapDirection = 1;
        return 1f;
    }
}

@Override
public boolean canScroll( int direction ) {
        RectF bitmapRect = getBitmapRect();
        updateRect( bitmapRect, mScrollRect );
        Rect imageViewRect = new Rect();
        getGlobalVisibleRect( imageViewRect );

        if( null == bitmapRect ) {
                return false;
        }

        if ( Math.abs( bitmapRect.right - imageViewRect.right ) < SCROLL_DELTA_THRESHOLD ) {
                if ( direction < 0 ) {
                        return false;
                }
        }

        if (Math.abs( bitmapRect.left - mScrollRect.left ) < SCROLL_DELTA_THRESHOLD) {
            if ( direction > 0 ) {
                return false;
            }
        }
        return true;
}

@Override
public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY ) {
    if ( getScale() == 1f ) return false;
    if (distanceX!=0 && !canScroll((int) -distanceX)) {
        getParent().requestDisallowInterceptTouchEvent(false);
        return false;
    } else {
        getParent().requestDisallowInterceptTouchEvent(true);
        mUserScaled = true;
        scrollBy( -distanceX, -distanceY );
        invalidate();
        return true;
   }
}
}