Android opengles animated text logic?

此生再无相见时 提交于 2019-12-13 05:18:13

问题


I have gotten text to render using opengl es on android and currently am trying to find out how to "animate" it like in pokemon games where it "reveals" the characters from left to right at a certain speed. How is this done?


回答1:


Basically, this "text-sliding-in" is like all other animations.

For example, look at this sample code:

public class GameObject {
    // Each element in this char array contains
    // a single character, representing a serie of text.
    private char[] mText;
    // Frames before a new character appears.
    private int mFrames;        
    // Current frame.
    private int mCurrentFrame;
    // Current index (which character is currently the last).
    private int mIndex;

    public GameObject(String defaultText, int framesPerCharacter) {
        final int textLength = defaultText.length();
        mText = new char[textLength];

        for (int x = 0; x < textLength; x++) {
            mText[x] = defaultText.charAt(x);
        }

        mFrames = framesPerCharacter;
    }

    public void drawText() {
        // I do not have room enough to explain drawing APIs, but 
        // you'll get the idea.
        for (int x = 0; x < mIndex; x++) {
            // Draw text, from the beginning to the current index.
            // Depending on the drawing API, you might have to
            // change the x and y coordinates for each character.
            APIDrawText.drawText(mText[x]);
        }

        // Reset the counter if the character's "animation"
        // is done and add one to the index. 
        // Otherwise, add one to the current frame.
        if (mCurrentFrame >= mFrames) { mCurrentFrame = 0; mIndex++; }
        else { mCurrentFrame++; }

        if (mIndex >= mText.length) {
            // Reset the index counter (will display the text all over again).
            mIndex = 0;
        }
    }
}

Notice that a game object class has more fields describing them, but for example purposes this should be enough.

/**
 * Basic OpenGL ES implementation on Android.
 * Should contain onSurfaceCreated() and onSurfaceChanged().
 */
public class GLRenderer extends GLSurfaceView implements Renderer {
    private GameObject mGameObject;

    public GLRenderer() { 
        // Add default text and add 25 frames per character.
        mGameObject = new GameObject("Default text!", 25);
    }

    /**
     * The ordinary draw function on Android. Your code should
     * look something similiar to this.
     */
    @Override
    public void onDrawFrame(GL10 gl) {
        // Use the method which you got to render text with OpenGL
        // here.
        mGameObject.drawText();         
    }
}

Well, what happens? To summarize:

First frame: D <- Increase mCurrentFrame by one.

Second frame: D <- Increase mCurrentFrame by one.

...

Twenty-sixth frame: De <- mIndex has increased to one (it loop through the mText variable two times).

...

All text has been displayed: <- Reset the mIndex to zero, reset mCurrentFrame to zero. This will play the animation from the beginning.

This is the basic idea. You can add more methods where you change the frames per character amount, change text, slow/speed up the animation after each character, etc.

I also wrote an example of this, but for the Canvas system. It should be easy for you to adapt to whatever you choose.

You can find my example here.



来源:https://stackoverflow.com/questions/6544969/android-opengles-animated-text-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!