android开发 添加动画,添加动画  |  Android 开发者  |  Android Developers

在屏幕上绘制对象是 OpenGL 的基本功能,但您也可以使用其他 Android 图形框架类(包括

在本课中,您将学习如何通过旋转为形状添加动画效果,从而进一步利用 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);

}

如果您的三角形在进行这些更改后没有旋转,请确保已对

启用连续渲染

如果您认真遵循了此类中针对这一点的示例代码,请确保对将渲染模式设置为仅在脏时才进行绘制的行取消备注,否则 OpenGL 仅按一个增量旋转形状,然后就等待从

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

}

除非您在没有任何用户互动的情况下更改对象,否则通常最好启用此标记。请准备好对此代码取消备注,因为下一课将使此调用再次适用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值