如何制作一个自定义的ScrollView来移动所有面。以及如何找到在andengine中点击ScrollView的位置?

提前致谢。

最佳答案

我写了一个小的ShapeScrollContainer.java类,该类正在进行中,但是可以正常使用,欢迎您使用,

https://skydrive.live.com/redir?resid=EB5E1E510A150D4D!105

它允许用户在容器区域内滚动,添加到ShapeScrollContainer中的内容将自动进行相应移动。如果将内容移至ShapeScrollContainer的边界之外,则会将内容项的可见性设置为false(如稍后所述,当接近这些边界时,您还可以使其淡出内容)。

我提供了完整的Java文档,其中包含每种方法的说明。本质上,它扩展了Rectangle并实现了IScrollDetectorListener,IClickDetectorListener接口。只需将其添加到场景中,就像添加另一个Shape,

ShapeScrollContainer ScrollableArea = new ShapeScrollContainer(x, y, width, height, new IShapeScrollContainerTouchListener()
{

    @Override
    public void OnContentClicked(Shape pShape) {
        // TODO Auto-generated method stub

    }

});
mScene.registerTouchArea(ScrollableArea);
mScene.attachChild(ScrollableArea);


如果用户单击添加到ShapeScrollContainer的项,则将调用OnContentClicked接口方法。 pShape参数将是指向被单击的Shape的指针。 ShapeScrollContainer会移动内容而不是移动相机,因此尚未添加到容器中的任何其他精灵将不受影响。

然后,您只需调用ShapeScrollContainer.Add()方法即可添加您的Sprite,动画/平铺Sprite,矩形等。例如,一个ChangeableText,

final ChangeableText mMessage = new ChangeableText(x, y, mFont, "Scroll Me", HorizontalAlign.LEFT, 14);
mMessage.setVisible(true);
mMessage.setZIndex(10);
mMessage.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
mScene.attachChild(mMessage);

ScrollableArea.Add(mMessage);


添加完所有内容后,ShapeScrollContainer将具有多种方法来满足您的需求,

//Whether you want to allow user to scroll vertical/horizontal
ScrollableArea.SetScrollableDirections(false, true);
//Only allow the user to scroll in a direction to available content
//(no more content in that direction - the user will be prevented from scrolling)
ScrollableArea.SetScrollLock(true);
//By how much over the last content in any direction the user is allowed to scroll (% of height/width)
ScrollableArea.SetAlphaPadding(10.0f, 0);
//Allow ShapeScrollContainer to increase alpha of contents and by what distance it starts inside
//the ShapeScrollContainer itself. (Fades content as it approaches the edges due to user scrolling)
ScrollableArea.SetScrollLockPadding(50.0f,0.0f);
//Whether scroll bars will be visible, (horizontal/vertical)
ScrollableArea.SetScrollBarVisibitlity(true,true)
//...
//A lot more methods to refine the ScrollableArea appearence and behaviour - see java doc


希望这是有用的。

关于java - andengine中的自定义ScrollView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16560616/

10-10 23:23