Making Android's ZoomButtonsController and Ges

2019-08-03 07:36发布

问题:

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.

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.

I finally came up with a "fix": if OnZoomListener.onVisibilityChanged() fires to invisible I call myScrollView's setOnTouchListener() to restore the gestureListener (like I did in onCreate()). The gestures work fine again.

Edit: 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...

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)?

回答1:

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.

Concept:

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 - 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.

Code with example:

    //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);

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
    }
};

Good luck, Luis