ViewPager amp; ImageView zooming issue(ViewPager amp;ImageView 缩放问题)
问题描述
我正在尝试使用 ViewPager 实现图像库.此外,为了实现缩放功能,我正在使用来自 github 的 TouchImageView.我也尝试过使用 ZoomableImageView.
I am trying to implement a Gallery of images using ViewPager. Also, to implement zoom feature in that, I am using TouchImageView from github. I have also tried using ZoomableImageView.
但是,问题是,如果我缩放图像 &如果我滚动图像,那么 ViewPager
会滚动而不是图像ViewPager
的下一个视图正在加载.
But, the problem is, if I zoom the image & if I scroll the image , then instead of image, ViewPager
is getting scrolled & next view of ViewPager
is getting loaded.
如果我缩放图像然后如果我滚动它,那么图像必须移动而不是 ViewPager
If I zoom the image then if I scroll that, then image has to move instead of ViewPager
ViewPager 的下一个视图只有在到达缩放图像的末尾时才需要加载.该怎么做?
ViewPager's next view has to load only if reach the end of the zoomed image. How to do that?
我找不到那个.如果我触摸 &对角拖动图像,然后图像才会移动.否则 ViewPager 的拖动被调用.
I am not able to find that.If I touch & drag the image diagonally, only then image is getting moved. or else ViewPager's drag is getting invoked.
ps:这不是重复的.缩放完成.但问题是在图像缩放之后.
ps: this is not duplicate. Zooming is done. But the problem is after image zooming.
推荐答案
是的,我也有同样的问题,不是 TouchImageView.
Yes I too had the same problem not with TouchImageView.
解决了当我的视图获得焦点时禁用 ViewPager 的问题.
Too solved the problem what i did is disabled the ViewPager when my view is getting the focus.
public class EnableDisableViewPager extends ViewPager {
private boolean enabled = true;
public EnableDisableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(enabled)
return super.onInterceptTouchEvent(arg0);
return false;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
所以在 TouchImageView 中实现你的监听器来触发一个事件,无论是缩放还是拖动.
so in TouchImageView implement your listener to trigger an event whether its zooming or dragging.
在您的 Activity 中为您的视图对象设置监听器.因此,当这些事件发生时,只需禁用视图寻呼机.
set listener to your view object in your Activity. So when those event occur just disable the view Pager.
注意:您还需要一个鼠标向上事件来启用 viewpager.
Note: you will also need a mouse up event to enable the viewpager.
已编辑
这仅适用于缩放,因此要让 ViewPager 滑动页面,您应该缩放回原始页面.
This will work only for Zoom, so for ViewPager to swipe pages you should zoom back to original.
将这些代码添加到您的 TouchImageView
Add these code to your TouchImageView
public class TouchImageView extends ImageView {
...
private TouchEventListener touchEventListener;
private void sharedConstructing(Context context) {
...
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
...
case MotionEvent.ACTION_UP:
...
if(touchEventListener != null)
{
if(saveScale == 1.0)
touchEventListener.onZoomToOriginal();
else
touchEventListener.onZoom();
}
break;
...
}
...
}
});
}
...
public TouchEventListener getTouchEventListener() {
return touchEventListener;
}
public void setTouchEventListener(TouchEventListener touchEventListener) {
this.touchEventListener = touchEventListener;
}
public interface TouchEventListener
{
void onZoom();
void onZoomToOriginal();
}
}
更好的解决方案
我们可以通过使用下面给出的方法,在不将 ViewPager 扩展到新类的情况下实现这一点.
We could achieve this without extending ViewPager to a new Class by using the method given below.
requestDisallowInterceptTouchEvent(true);
有了这个,我们可以在不缩小到原始位置的情况下滑动,就像我们在图库和许多其他应用程序中看到的那样.
And with this we could swipe without zooming out to original position as we see in Gallery and many other apps.
public class TouchImageView extends ImageView {
...
private void stopInterceptEvent()
{
getParent().requestDisallowInterceptTouchEvent(true);
}
private void startInterceptEvent()
{
getParent().requestDisallowInterceptTouchEvent(false);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
stopInterceptEvent();
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
if(deltaX == 0)
startInterceptEvent();
else
stopInterceptEvent();
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
startInterceptEvent();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
}
这篇关于ViewPager &ImageView 缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ViewPager &ImageView 缩放问题
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01