使用SurfaceScrollDetector,PinchZoomDetector进行andengine平滑滚动和缩放

时间:2013-05-15 16:07:06

标签: java android andengine

我正在尝试使用SurfaceScrollDetector和PinchZoomDetector实现平滑的TMX地图滚动和缩放。

这是我的代码:

public class TMXTiledMapExample extends SimpleBaseGameActivity implements IOnSceneTouchListener, IScrollDetectorListener, IPinchZoomDetectorListener {

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private SmoothCamera camera;

private TMXTiledMap mTMXTiledMap;

private SurfaceScrollDetector mScrollDetector;
private PinchZoomDetector mPinchZoomDetector;
private float mPinchZoomStartedCameraZoomFactor;

@Override
public EngineOptions onCreateEngineOptions() {
    this.camera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 400, 400, 10f);
    final CroppedResolutionPolicy canvasSurface = new CroppedResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, canvasSurface, this.camera);

    if (MultiTouch.isSupported(this)) {
        if (MultiTouch.isSupportedDistinct(this)) {
            Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG)
                    .show();
        }
    } else {
        Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
    }

    return engineOptions;
}

@Override
public void onCreateResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
}

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.setOnAreaTouchTraversalFrontToBack();

    this.mScrollDetector = new SurfaceScrollDetector(this);
    this.mPinchZoomDetector = new PinchZoomDetector(this);

    scene.setOnSceneTouchListener(this);
    scene.setTouchAreaBindingOnActionDownEnabled(true);

    try {
        final TMXLoader tmxLoader = new TMXLoader(this.getAssets(), this.mEngine.getTextureManager(), TextureOptions.BILINEAR, this.getVertexBufferObjectManager(),
                new ITMXTilePropertiesListener() {
                    @Override
                    public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile,
                            final TMXProperties<TMXTileProperty> pTMXTileProperties) {
                    }
                });
        this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/desert2.tmx");

    } catch (final TMXLoadException e) {
        Debug.e(e);
    }

    final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
    scene.attachChild(tmxLayer);

    this.camera.setBounds(0, 0, tmxLayer.getHeight(), tmxLayer.getWidth());
    this.camera.setBoundsEnabled(true);

    return scene;
}

@Override
public void onScrollStarted(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY / zoomFactor);
}

@Override
public void onScroll(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY / zoomFactor);
}

@Override
public void onScrollFinished(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY / zoomFactor);
}

@Override
public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent) {
    this.mPinchZoomStartedCameraZoomFactor = this.camera.getZoomFactor();
}

@Override
public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
    this.camera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);
}

@Override
public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
    this.camera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);
}

@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
    this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

    if (this.mPinchZoomDetector.isZooming()) {
        this.mScrollDetector.setEnabled(false);
    } else {
        if (pSceneTouchEvent.isActionDown()) {
            this.mScrollDetector.setEnabled(true);
        }
        this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
    }

    return true;
}
}

滚动和放大此代码并不是很顺利。它的问题在哪里?如何创建漂亮的惯性滚动?

缩放的另一个问题。如何使用min和max zoomfactor限制它?我应该在IPinchZoomDetectorListener方法中放置一个特殊条件,还是可以通过提供给我的相机对象的一些参数来实现它?

此外,最后三个SmoothCamera参数的“正常”值是什么 - final float pMaxVelocityX,final float pMaxVelocityY,final float pMaxZoomFactorChange?

1 个答案:

答案 0 :(得分:3)

之前我做过类似的工作,所以我可以帮助你实现目标。

首先,如果你想放大/缩小功能,那么你必须使用ZoomCamera,你必须只提供宽度和高度,所有其他参数都默认使用。

    mZoomCamera = new ZoomCamera(0, 0, Constants.CAMERA_WIDTH,
            Constants.CAMERA_HEIGHT);
    mZoomCamera.setBounds(0f, 0f, Constants.CAMERA_WIDTH,
            Constants.CAMERA_HEIGHT);
    mZoomCamera.setBoundsEnabled(true);

在示例代码中,您将看到我如何为摄像机设置边界,因此摄像机无法完成此操作。而且,由于你提供的速度不合适,我觉得你的行为异常。

要设置特定的缩放值,您可以使用以下代码

private void setCameraZoomFactor(float pZoomFactor) {
    newZoomFactor = mPinchZoomStartedCameraZoomFactor * pZoomFactor;
    if (newZoomFactor < 1f)
        newZoomFactor = 1f;
    if (newZoomFactor > 1.5f)
        newZoomFactor = 1.5f;

    mZoomCamera.setZoomFactor(newZoomFactor);
}

您必须在onPinchZoom方法中调用此方法。

我想我现在回答了你所有的问题。