添加动画
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在屏幕上绘制对象是 OpenGL 的一个基本功能,
Android 图形框架类,包括 Canvas
和
Drawable
对象。OpenGL ES 为
在三维空间内移动和转换绘制的对象,或者以其他独特的方式
打造富有吸引力的用户体验。
在本课中,您将学习如何添加动画效果,从而更进一步使用 OpenGL ES
旋转为一个具有旋转的形状。
旋转形状
使用 OpenGL ES 2.0 旋转绘制的对象相对比较简单。在渲染程序中,创建
然后将另一个转换矩阵(旋转矩阵)再与您的投影和
相机视图转换矩阵:
Kotlin
private val rotationMatrix = FloatArray(16)
override fun onDrawFrame(gl: GL10) {
val scratch = FloatArray(16)
...
// Create a rotation transformation for the triangle
val time = SystemClock.uptimeMillis() % 4000L
val angle = 0.090f * time.toInt()
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1.0f)
// Combine the rotation matrix with the projection and camera view
// Note that the vPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0)
// Draw triangle
mTriangle.draw(scratch)
}
Java
private float[] rotationMatrix = new float[16];
@Override
public void onDrawFrame(GL10 gl) {
float[] scratch = new float[16];
...
// Create a rotation transformation for the triangle
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
Matrix.setRotateM(rotationMatrix, 0, angle, 0, 0, -1.0f);
// Combine the rotation matrix with the projection and camera view
// Note that the vPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0);
// Draw triangle
mTriangle.draw(scratch);
}
如果您的三角形在进行这些更改后没有旋转,请确保您已对
GLSurfaceView.RENDERMODE_WHEN_DIRTY
具体设置,如下一部分所述。
启用连续渲染
如果您到目前为止认真阅读了本课程中的示例代码,
请务必注释掉用于将渲染模式设置为仅在 dirty 时绘制的代码行,否则 OpenGL
将形状仅旋转 1 个增量,然后等待从 GLSurfaceView
容器调用 requestRender()
:
Kotlin
class MyGLSurfaceView(context: Context) : GLSurfaceView(context) {
init {
...
// Render the view only when there is a change in the drawing data.
// To allow the triangle to rotate automatically, this line is commented out:
// renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
}
}
Java
public class MyGLSurfaceView(Context context) extends GLSurfaceView {
...
// Render the view only when there is a change in the drawing data.
// To allow the triangle to rotate automatically, this line is commented out:
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
除非对象在没有任何用户互动的情况下发生改变,否则通常最好采取以下做法:
标记。请准备好对此代码取消注释,因为下一课会让此调用适用
。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Add motion\n\nDrawing objects on screen is a pretty basic feature of OpenGL, but you can do this with other\nAndroid graphics framework classes, including [Canvas](/reference/android/graphics/Canvas) and\n[Drawable](/reference/android/graphics/drawable/Drawable) objects. OpenGL ES provides additional capabilities for\nmoving and transforming drawn objects in three dimensions or in other unique ways to create\ncompelling user experiences.\n\nIn this lesson, you take another step forward into using OpenGL ES by learning how to add motion\nto a shape with rotation.\n\nRotate a shape\n--------------\n\nRotating a drawing object with OpenGL ES 2.0 is relatively simple. In your renderer, create\nanother transformation matrix (a rotation matrix) and then combine it with your projection and\ncamera view transformation matrices: \n\n### Kotlin\n\n```kotlin\nprivate val rotationMatrix = FloatArray(16)\n\noverride fun onDrawFrame(gl: GL10) {\n val scratch = FloatArray(16)\n\n ...\n\n // Create a rotation transformation for the triangle\n val time = SystemClock.uptimeMillis() % 4000L\n val angle = 0.090f * time.toInt()\n Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1.0f)\n\n // Combine the rotation matrix with the projection and camera view\n // Note that the vPMatrix factor *must be first* in order\n // for the matrix multiplication product to be correct.\n Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0)\n\n // Draw triangle\n mTriangle.draw(scratch)\n}\n```\n\n### Java\n\n```java\nprivate float[] rotationMatrix = new float[16];\n@Override\npublic void onDrawFrame(GL10 gl) {\n float[] scratch = new float[16];\n\n ...\n\n // Create a rotation transformation for the triangle\n long time = SystemClock.uptimeMillis() % 4000L;\n float angle = 0.090f * ((int) time);\n Matrix.setRotateM(rotationMatrix, 0, angle, 0, 0, -1.0f);\n\n // Combine the rotation matrix with the projection and camera view\n // Note that the vPMatrix factor *must be first* in order\n // for the matrix multiplication product to be correct.\n Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0);\n\n // Draw triangle\n mTriangle.draw(scratch);\n}\n```\n\nIf your triangle does not rotate after making these changes, make sure you have commented out the\n[GLSurfaceView.RENDERMODE_WHEN_DIRTY](/reference/android/opengl/GLSurfaceView#RENDERMODE_WHEN_DIRTY)\nsetting, as described in the next section.\n\nEnable continuous rendering\n---------------------------\n\nIf you have diligently followed along with the example code in this class to this point, make\nsure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL\nrotates the shape only one increment and then waits for a call to [requestRender()](/reference/android/opengl/GLSurfaceView#requestRender()) from the [GLSurfaceView](/reference/android/opengl/GLSurfaceView) container: \n\n### Kotlin\n\n```kotlin\nclass MyGLSurfaceView(context: Context) : GLSurfaceView(context) {\n\n init {\n ...\n // Render the view only when there is a change in the drawing data.\n // To allow the triangle to rotate automatically, this line is commented out:\n // renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY\n }\n}\n```\n\n### Java\n\n```java\npublic class MyGLSurfaceView(Context context) extends GLSurfaceView {\n ...\n // Render the view only when there is a change in the drawing data.\n // To allow the triangle to rotate automatically, this line is commented out:\n //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);\n}\n```\n\nUnless you have objects changing without any user interaction, it's usually a good idea have this\nflag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable\nonce again."]]