要用OpenGL画一个流星动画,你需要遵循以下步骤:

  1. 创建一个新的Android项目,并在build.gradle文件中添加OpenGL ES的依赖项。
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.1.0'
implementation 'com.google.android.gms:play-services-ads:18.2.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.1.0'
implementation 'androidx.lifecycle:lifecycle-service:2.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.room:room-runtime:2.2.2'
implementation 'androidx.room:room-ktx:2.2.2'
implementation 'androidx.room:room-compiler:2.2.2'
implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.1.0'
implementation 'androidx.lifecycle:lifecycle-runtime:2.1.0'
implementation 'androidx.lifecycle:lifecycle-process:2.1.0'
implementation 'androidx.lifecycle:lifecycle-common:2.1.0'
implementation 'androidx.lifecycle:lifecycle-reactivestreams:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-scalars:2.6.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2'
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
implementation 'com.github.bumptech.glide:glide:4.10.0'
  1. 在AndroidManifest.xml文件中添加OpenGL ES版本的要求。
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
  1. 创建一个自定义的GLSurfaceView类,并重写onSurfaceCreated、onSurfaceChanged和onDrawFrame方法。
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyGLSurfaceView extends GLSurfaceView {

    private final MyGLRenderer renderer;

    public MyGLSurfaceView(Context context) {
        super(context);

        setEGLContextClientVersion(2);

        renderer = new MyGLRenderer();

        setRenderer(renderer);
    }

    private static class MyGLRenderer implements GLSurfaceView.Renderer {

        private Star[] stars;

        @Override
        public void onSurfaceCreated(GL10 unused, EGLConfig config) {
            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            stars = new Star[50];
            for (int i = 0; i < stars.length; i++) {
                stars[i] = new Star();
            }
        }

        @Override
        public void onSurfaceChanged(GL10 unused, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
        }

        @Override
        public void onDrawFrame(GL10 unused) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

            for (Star star : stars) {
                star.draw();
            }
        }
    }
}
  1. 创建一个Star类来表示流星,并在其draw方法中使用OpenGL绘制流星。
import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Star {

    private FloatBuffer vertexBuffer;

    private final int vertexCount = 5;
    private final int vertexStride = 5 * 4;

    private final float[] vertices = {
            0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
            -0.2f, -0.5f, 0.0f, 0.0f, 1.0f,
            0.2f, -0.5f, 0.0f, 0.0f, 1.0f,
            0.0f, 0.5f, 0.0f, 0.0f, 1.0f,
            -0.2f, 0.0f, 0.0f, 0.0f, 1.0f
    };

    public Star() {
        ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
        bb.order(ByteOrder.nativeOrder());

        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);
    }

    public void draw() {
        GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
        GLES20.glEnableVertexAttribArray(0);

        GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
        GLES20.glEnableVertexAttribArray(1);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertexCount);
    }
}
  1. 在MainActivity中使用MyGLSurfaceView来显示OpenGL动画。
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class MainActivity extends Activity {

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}

现在你可以运行你的应用程序,并在OpenGL窗口中看到流星动画了


原文地址: http://www.cveoy.top/t/topic/iZLC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录