Android打开动画文字逻辑?

时间:2011-07-01 07:35:08

标签: android text opengl-es

我已经获得了使用opengl es在android上渲染的文本,目前我正在尝试找出如何在宠物小精灵游戏中“动画化”它,它以一定的速度从左到右“显示”角色。 这是怎么做到的?

1 个答案:

答案 0 :(得分:1)

基本上,这种“文字滑入”就像所有其他动画一样。

例如,请查看此示例代码:

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;
        }
    }
}

请注意,游戏对象类有更多描述它们的字段,但出于示例目的,这应该足够了。

/**
 * 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();         
    }
}

嗯,会发生什么?总结一下:

  

第一帧:D&lt; - 将 mCurrentFrame 增加一个。

     

第二帧:D&lt; - 将 mCurrentFrame 增加一个。

     

<强> ...

     

第二十六帧:De&lt; - mIndex 已增加到一个(它循环通过   mText变量两次)。

     

<强> ...

     

所有文字都已显示:&lt; - 将 mIndex 重置为零,重置 mCurrentFrame   为零。这将从头开始播放动画。

这是基本的想法。您可以添加更多方法来更改每个字符数量的帧数,更改文本,减慢/加速每个字符后的动画等。

我也写了一个这样的例子,但对于Canvas系统。您应该很容易适应您的选择。

您可以找到我的示例here

相关问题