Braindrop Labs Apps

This post assumes an intermediate knowledge of AndEngine and Java for Android. The assumption that the Scene and Engine have been initialized is also made.

Introduction

I’ve been working and experimenting with AndEngine for a little over a year now and I wanted to share some information regarding the “Pinch and Zoom” functionality built into the engine. The engine itself makes it pretty easy to utilize multi-touch pinch and zoom, but documentation explaining how to do so is lacking. There are examples which will help developers understand the intended purposes of many of the engine’s core concepts, and they can be found at http://code.google.com/p/andengineexamples/ .

Anyway, for my implementation I created the class TouchAndCameraManager which, as you might guess, helps me manage user touches and the camera’s reactive behavior. Below are the core steps needed to complete pinch and zoom in Android using AndEngine.

Step 1 – Interface Implementation

You’ll first need to implement the following interfaces in your desired class.

  • IOnSceneTouchListener
  • IPinchZoomDetectorListener
public class TouchAndCameraManager implements IOnSceneTouchListener, IPinchZoomDetectorListener

Step 2 – Initialize PinchZoomDetector Instance

Next, you need to create the PinchZoomDetector instance, I’ve named it mPinchZoomDetector. I’ve chosen to create my instance in my initialize() method, but this can be done in the constructor or any other method you chose for that matter.

// MultiTouch check
if (MultiTouch.isSupportedByAndroidVersion())
{
	try
	{
		this.mPinchZoomDetector = new PinchZoomDetector(this);
	}
	catch (final MultiTouchException e)
	{
		this.mPinchZoomDetector = null;
	}
}
else
{
	this.mPinchZoomDetector = null;
}

Step 3 – Initialize SmoothCamera/ZoomCamera Instance

Next, you need to initialize your SmoothCamera instance, I’ve named mine mCamera. SmoothCamera extends ZoomCamera by the way.

// SMOOTH_CAMERA_SPEED and SMOOTH_CAMERA_ZOOM_FACTOR_CHANGE are values you can play with to adhere to your liking.
mCamera = new SmoothCamera(0, 0, mPhysicalScreenWidth, mPhysicalScreenHeight, SMOOTH_CAMERA_SPEED, 0, SMOOTH_CAMERA_ZOOM_FACTOR_CHANGE);
mCamera.setBounds(0, mPhysicalScreenWidth, 0, mPhysicalScreenHeight);
mCamera.setBoundsEnabled(true);
mCamera.setZoomFactor(1f);

Step 4 – Override Methods

Next, you’ll need to override the following methods of the two implemented interfaces. The methods are

  • onPinchZoomStarted
  • onPinchZoom
  • onPinchZoomFinished
  • onSceneTouchEvent
@Override
public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent)
{
	// mPinchZoomStartFactor is calculated on start as it is used in the onPinchZoom() method
	mPinchZoomStartFactor = mCamera.getZoomFactor();
}

@Override
public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor)
{
	if (pZoomFactor != 1)
	{
		// check bounds
		float newZoomFactor = mPinchZoomStartFactor * pZoomFactor;
		if (newZoomFactor <= mZoomMin)
			mCamera.setZoomFactor(mZoomMin);
		else if (newZoomFactor >= ZOOM_MAX)
			mCamera.setZoomFactor(ZOOM_MAX);
		else
			mCamera.setZoomFactor(newZoomFactor);
	}
}

@Override
public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor)
{
	// enable desired functionality because zooming is complete
}

@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent)
{
	if (mPinchZoomDetector != null)
	{
		mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

		if (mPinchZoomDetector.isZooming())
		{
			// disable functionality that shouldn't occur while zooming
		}
		else
		{
			// enable functionality that should occur while zooming
		}
	}

	return true;
}

Conclusion

Following the steps above will help you utilize AndEngine’s “Pinch and Zoom” functionality with ease. Feel free to experiment with the code snippets and/or ask any questions relating to this post.

Leave a Reply