使Android的ZoomButtonsController和GestureDetector相处融洽 [英] Making Android's ZoomButtonsController and GestureDetector get along

查看:78
本文介绍了使Android的ZoomButtonsController和GestureDetector相处融洽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经坚持了一段时间,尽管我找到了解决方法,但我不太理解为什么为什么,并且我应该实施它适当地!那里的ZoomButtonsController上似乎也缺少信息.

I've been stuck on this for a while now, and though I found a workaround I don't quite understand why it works and I should to implement it properly! There also seems to be a dearth of information on the ZoomButtonsController out there.

在这里的其他文章中,我已经在我的应用程序中实现了滑动"手势功能.但是,我也希望通过单击来以编程方式显示缩放按钮.我已经为此ScrollView使用基于GestureDetector的OnTouchListener,因此也为其添加了一个ZoomButtonsController作为OnZoomListener(以及用于处理onSingleTapConfirmed和其他此类代码的代码).

Per other articles here, I've implemented "swipe" gesture functionality in my app. However, on a single tap, I also wanted the zoom buttons to appear programmatically. I had the GestureDetector-based OnTouchListener already for this ScrollView, and so I added a ZoomButtonsController as the OnZoomListener for it as well (along with code to handle onSingleTapConfirmed and other such things).

一切正常-直到出现缩放按钮.从那时起(假设能见度始终保持不变),即使缩放按钮消失,手势也不会起作用,甚至无法轻按!您可以在屏幕上显示缩放按钮时单击它们,并且滚动仍然可以正常进行,但是手势消失了.

Things worked fine - until the zoom buttons appeared. From that point forward (assuming constant visibility), no gestures work, not even tapping, even after the zoom buttons fade away! You can click the zoom buttons while they are onscreen and scroll still works fine but the gestures are gone.

我终于想出了一个解决方案":如果OnZoomListener.onVisibilityChanged()触发为不可见,我将调用myScrollView的setOnTouchListener()来还原手势侦听器(就像我在onCreate()中所做的那样).手势再次正常工作.

如果在onVisibilityChanged()触发可见时执行此操作,则手势会立即起作用,但会禁用缩放按钮,因此效果并不理想!两者都很好...

if you do this when onVisibilityChanged() fires to visible, you get gestures working right away BUT it disables the zoom buttons so it's not that great! It'd be nice to have both...

那么,我在使用ZoomButtonsController的正确方法是正确的吗?如果不是,那是什么?更重要的是,为什么当出现缩放按钮时它们似乎可以永久替代我的OnTouchListener?是否应让ZoomButtonsController 在其触发后劫持当前的手势?比这(监听器的某些常规属性)更基本吗?

So, is what I'm doing the right way to use ZoomButtonsController and if not, what is? More importantly, why is it when the zoom buttons appear they seem to replace my OnTouchListener permanently? Is ZoomButtonsController supposed to hijack the gestures currently after it fires? Is this somehow more basic than that (some general property of listeners)?

推荐答案

在找到解决方案之前,我已经为这个问题苦苦挣扎了一段时间.我不确定这是否是最正确的,但功能是否正常.

I've struggled with this issue for a while before I've found a solution. I'm not sure if it's the most correct one, but it's functioning perfectly.

概念:

1-不要将ZoomButtonsController小部件直接添加到需要它们的视图中. onTouch事件将发生冲突.相反,您需要将ZoomButtonsController小部件添加到新视图,并将此视图添加到要向其添加缩放按钮的视图所在的布局.

1 - Don´t add the ZoomButtonsController widget directly to the view where you need them. The onTouch events will conflict. Instead, you need to add the ZoomButtonsController widget to a new view and add this view to the same layout where is the view you want to add the zoom buttons to.

2-添加了ZoomButtonsController的视图必须是最后一个添加到父布局的视图.这样,它将是第一个被调用的方法,如果未按下缩放按钮,则会将onTouch事件传递给您的视图供您处理.

2 - The view where the ZoomButtonsController have been added must be the last being added to the parent layout. This way it will be the first being called, and if the zoom buttons are not pressed the onTouch event is passed to your view for you to process.

带有示例的代码:

    //First my view where I need the zoom buttons
    imageView = new ImageView(context);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    imageView.setLayoutParams(layoutParams);
    imageView.setOnTouchListener(onTouchListener);
    relativeLayout.addView(imageView);

    //Secondly the view where the buttons are added     
    View zoomView = new View(context);
    LayoutParams layoutParamsZoom = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    zoomView.setLayoutParams(layoutParamsZoom);
    relativeLayout.addView(zoomView);
    zoomButtonsController = new ZoomButtonsController(zoomView); 
    zoomButtonsController.setOnZoomListener(onZoomListener);

最后,您只需要将侦听器添加到zoom小部件中即可:

Finally, you just need to add the listener to the zoom widget:

ZoomButtonsController.OnZoomListener onZoomListener = new ZoomButtonsController.OnZoomListener() {

    @Override
    public void onZoom(boolean zoomIn) {
        Log.d(TAG, "onZoom: " +  zoomIn);
        if(zoomIn){ 
            setZoom(currentZoom +1); 
        }else{ 
            setZoom(currentZoom -1); 
        } 
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        // TODO Auto-generated method stub
    }
};

祝你好运, 路易斯

这篇关于使Android的ZoomButtonsController和GestureDetector相处融洽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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