Skip to content Skip to sidebar Skip to footer

Making Android's Zoombuttonscontroller And Gesturedetector Get Along

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 deart

Solution 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 = newImageView(context);
    LayoutParamslayoutParams=newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    imageView.setLayoutParams(layoutParams);
    imageView.setOnTouchListener(onTouchListener);
    relativeLayout.addView(imageView);

    //Secondly the view where the buttons are added     ViewzoomView=newView(context);
    LayoutParamslayoutParamsZoom=newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    zoomView.setLayoutParams(layoutParamsZoom);
    relativeLayout.addView(zoomView);
    zoomButtonsController = newZoomButtonsController(zoomView); 
    zoomButtonsController.setOnZoomListener(onZoomListener);

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

ZoomButtonsController.OnZoomListener onZoomListener = newZoomButtonsController.OnZoomListener() {

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

    @OverridepublicvoidonVisibilityChanged(boolean visible) {
        // TODO Auto-generated method stub
    }
};

Good luck, Luis

Post a Comment for "Making Android's Zoombuttonscontroller And Gesturedetector Get Along"