图像缩放/平移图库内 [英] Image zooming/panning inside of a Gallery

查看:117
本文介绍了图像缩放/平移图库内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缩放/移动图像在Android画廊部件。图像覆盖整个屏幕。虽然我可以放大/平移图​​像画廊,我不能够刷卡到下一个/ previous图像。缩放和平移的个别画面,工作得很好。

我创建了一个TouchImageView它与变焦和你好的Andr​​oid书平移的功能扩展ImageView的。然后,我在返回图像画廊适配器类的getView()方法返回该TouchImageView。

我发现在谷歌组完全相同的问题在 http://groups.google .COM /组/ Android的开发者/味精/ 97421179bfc5a3b2 但没有答复那边。<​​/ P>

感谢你。

解决方案

我通过从图库部件类派生的新类解决了这个。我打电话给我的ZoomableGallery。我有我的执行一些动作侦听器来处理缩放和双击变焦在非多点触摸或pre 2.0设备。

 公共类ZoomableGallery扩展库实现OnDoubleTapListener,OnGestureListener,OnScaleGestureListener {}
 

关键是没有你的内在部件消耗的触摸事件。这似乎是个好主意,以创建一个ZoomablePannableImageView响应触摸事件,放大和缩小。它似乎是一个好主意,因为这可重用组件将工作做好的画廊之外为好。不过,我不认为它可很好地工作。最好的方式是创建一个ZoomableImageView不处理触摸事件或设置任何gesturelisteners,而是提供一个api用于设定比例因子和平移在X,Y尺寸为常规方法

然后,一旦你已经走了这条路线,你可以有你的图库小部件的后代处理所有的触摸事件巧妙地只转发接触的运动需要进入插件的作品。如果说我们是放大到图像,我们观看了几乎整个图像,除了4个像素超出屏幕的左边是我的意思是。如果我们收到触摸事件滚动图像8像素的权利。我们的画廊部件需要发送的图像小部件泛4个像素正确的信息。然后它需要也消耗4个像素水平运动本身。这样不仅图像视图显示它是完整的左边缘,但随后幻灯片有点向右可能揭示画廊适配器下一个ImageView的。

关键功能的扩展库新的类重写为:

  @覆盖
公共布尔的onTouchEvent(MotionEvent事件){}
 

  @覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
        浮distanceY){}
 

我不想复制和粘贴我的整个实施,因为这是杂乱,很难理解,它也可以显示在压力下一些出问题的行为。但我不认为这是因为战略我雇而仅仅是因为我还没有清理它呢。

关键是要记住用户处于何种模式(缩放,平移,或在影像中,平移整个画廊)

在用户的平移这里的图像内的情况是我的code看起来像处理switch语句中的onScroll法里:

 案例INNERDRAG:
        浮unhandledPan = mCurrentPannable.panHorizo​​ntally(distanceX);

        //负未处理的泛意味着我们在转移到图像向右

        如果(unhandledPan!= 0.0){
            如果(unhandledPan&LT; 0.0)
                mMode服务= GALPANRIGHT;
            其他
                mMode服务= GALPANLEFT;

            返回super.onScroll(E1,e2,0.0f-unhandledPan,distanceY);

        } 其他 {
            返回true;
        }
 

在C mCurrentPannable此$ C $指的是当前选中通过画廊的看法。 Pannable就是这样panHorizo​​ntally和panVertically定义为函数做两件事情的接口:尝试通过如此多的像素和如果盘的量采取它超出了它可以平移的边沿以平移的内视图,它返回的像素数它无法处理。

然后画廊而不是传递相同的参数,以super.onScroll它通过只留下什么未使用的锅。

我希望这有助于。

I am trying to zoom/pan images in the android gallery widget. The images cover the full screen. Though I can zoom/pan images in the gallery, I am not able to swipe to the next/previous images. Zooming and panning for single image works fine.

I created a TouchImageView which extends ImageView with capability of zooming and panning from Hello Android book. Then I returned this TouchImageView in the getView() method of the Adapter class that returns images to the Gallery.

I found exactly same problem in google groups at http://groups.google.com/group/android-developers/msg/97421179bfc5a3b2 but no replies over there.

Thank you.

解决方案

I solved this by making a new class derived from Gallery widget class. I called mine ZoomableGallery. I had mine implement a few gesture listeners to handle scaling and double tap for zoom on non multi-touch or pre 2.0 devices.

public class ZoomableGallery extends Gallery implements OnDoubleTapListener, OnGestureListener, OnScaleGestureListener {}

The key is to not have your inner widgets consume the touch events. It might seem like the right idea to create a ZoomablePannableImageView that responds to touch events and zooms in and out. And it seems like a great idea because this reusable component would work well outside of the Gallery as well. However I don't think it can be made to work well. The best way is to create a ZoomableImageView that doesn't handle touch events or set any gesturelisteners, but instead provides an api for setting the scale factor and panning in the X,Y dimensions as regular methods.

Then once you've gone this route you can have your descendant of the Gallery widget handle all the touch events smartly only forwarding the pieces of the touch motion that need to go to the widget. And what I mean by this is if say we are zoomed into the image and we are viewing almost the whole image except 4 pixels are out of screen to the left. If we receive a touch event to scroll the image 8 pixels to the right. Our gallery widget needs to send the image widget a pan 4 pixels right message. And then it needs to also consume 4 pixels of horizontal motion itself. So that not only does the image view reveal it's complete left edge, but then it slides a little to the right possibly revealing the next imageview in the gallery adapter.

The key functions to override in your new class that extends Gallery are:

    @Override
public boolean onTouchEvent(MotionEvent event) {}

and

    @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {}

I don't want to copy and paste my entire implementation because it is messy and hard to understand and it also can display some glitchy behavior under stress. But I don't think that's because of the strategy I employed but simply because I haven't cleaned it up yet.

The key is to remember what mode the user is in (zooming, or panning inside an image, panning the entire gallery)

In the case where the user is panning inside an image here is what my code looks like handling the switch statement inside of the onScroll method:

        case INNERDRAG:
        float unhandledPan = mCurrentPannable.panHorizontally(distanceX);

        // Negative unhandled pan means that we are shifting over to the image to the right

        if (unhandledPan != 0.0f) {
            if (unhandledPan < 0.0f)
                mMode = GALPANRIGHT;
            else
                mMode = GALPANLEFT;

            return super.onScroll(e1,e2,0.0f-unhandledPan,distanceY);

        } else {
            return true;
        }       

In this code mCurrentPannable refers to the view that is currently "selected" by the gallery. Pannable is just an interface that defines panHorizontally and panVertically as functions that do two things: attempt to pan the inner view by so many pixels AND if that amount of pan takes it beyond the edges of what it can pan, it returns the number of pixels that it couldn't handle.

Then the gallery instead of passing the same arguments to super.onScroll it passes only what is left unconsumed by the pan.

I hope this helps.

这篇关于图像缩放/平移图库内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆