Custom ScrollView in andengine

后端 未结 3 727
难免孤独
难免孤独 2021-01-25 08:38

How can I make a custom ScrollView which moves all the sides. And how do I also find the position where I tapped the ScrollView in andengine?

T

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 09:16

    I wrote a small ShapeScrollContainer.java class which is a work in progress but is functional and you are welcome to use,

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

    It allows a user to scroll within the container area, contents which you add to the ShapeScrollContainer automatically get moved accordingly. If content is moved outside of the bounds of the ShapeScrollContainer, it will set the content item visibility to false, (as described later you can also get it to fade out the content as it approaches these bounds).

    I have included full java doc with explanations for each method. Essentially it extends Rectangle and implements the IScrollDetectorListener, IClickDetectorListener interfaces. Simply add it to your scene as you would another 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);
    

    The OnContentClicked interface method will get called if an item which you added to the ShapeScrollContainer gets clicked by a user. The pShape argument will be a pointer to the Shape which was clicked. The ShapeScrollContainer moves the contents not the camera so any other sprites you have not added to the container will appear unaffected.

    You then just call the ShapeScrollContainer.Add() method to add your Sprites, Animated/Tiled Sprites, Rectangles e.t.c. For example, a 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);
    

    Once you have added everything the ShapeScrollContainer has a variety of methods to tailor it to your needs,

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

    Hope this is of use.

提交回复
热议问题