美好的一天。我非常沮丧,因为我想要实现的是世界上最简单的东西,但我无处可获得任何东西。我只是想在GL表面视图的中心画一条简单的HORIZONTAL线(想法是根据录制的幅度制作音频波动画)所以我用帆布简单轻松地实现了这一点,但是GL表面视图关于如何在gl的表面上精确绘制该线,甚至没有一个简单的例子或一个简单的问题。
无论如何,这里是我得到的,并且不知道我应该在哪里编写绘图代码以及我应该写什么来实现上述期望的功能。
GL渲染类
public class MyGLRenderer implements GLSurfaceView.Renderer {
GL10 unused;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
// GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
this.unused = unused;
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
GL View类(此类是从另一个扩展SurfaceView的副本粘贴的,所以不要注意画布等等。它们将被删除)
public class GLView extends GLSurfaceView {
// The number of buffer frames to keep around (for a nice fade-out visualization).
private static final int HISTORY_SIZE = 1;
private MyGLRenderer mRenderer;
Handler handler = new Handler();
private boolean isFinished=true;
private Runnable runnable = new Runnable() {
@Override
public void run() {
int colorDelta = 255 / (HISTORY_SIZE + 1);
int brightness = colorDelta;
final Random rnd = new Random();
mPaint.setColor(Color.argb(brightness, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
isFinished = true;
brightness += colorDelta;
}
};
// To make quieter sounds still show up well on the display, we use +/- 8192 as the amplitude
// that reaches the top/bottom of the view instead of +/- 32767. Any samples that have
// magnitude higher than this limit will simply be clipped during drawing.
private static final float MAX_AMPLITUDE_TO_DRAW = 10000.0f;
// The queue that will hold historical audio data.
private final LinkedList<short[]> mAudioData;
private final Paint mPaint;
public GLView(Context context) {
super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
mAudioData = new LinkedList<short[]>();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(5);
mPaint.setAntiAlias(true);
mRenderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(mRenderer);
}
public GLView(Context context, AttributeSet attrs) {
super(context, attrs);
mAudioData = new LinkedList<short[]>();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(5);
mPaint.setAntiAlias(true);
mRenderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(mRenderer);
}
/**
* Updates the waveform view with a new "frame" of samples and renders it. The new frame gets
* added to the front of the rendering queue, pushing the previous frames back, causing them to
* be faded out visually.
*
* @param buffer the most recent buffer of audio samples
*/
public synchronized void updateAudioData(short[] buffer) {
short[] newBuffer;
// We want to keep a small amount of history in the view to provide a nice fading effect.
// We use a linked list that we treat as a queue for this.
if (mAudioData.size() == HISTORY_SIZE) {
newBuffer = mAudioData.removeFirst();
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
} else {
newBuffer = buffer.clone();
}
mAudioData.addLast(newBuffer);
final Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
drawWaveform(canvas);
getHolder().unlockCanvasAndPost(canvas);
}
}
/**
* Repaints the view's surface.
*
* @param canvas the {@link Canvas} object on which to draw
*/
private void drawWaveform(Canvas canvas) {
// Clear the screen each time because SurfaceView won't do this for us.
canvas.drawColor(Color.BLACK);
float width = getWidth();
float height = getHeight();
float centerY = height / 2;
// We draw the history from oldest to newest so that the older audio data is further back
// and darker than the most recent data.
int colorDelta = 255 / (HISTORY_SIZE + 1);
int brightness = colorDelta;
final Random rnd = new Random();
if(isFinished){
isFinished = false;
handler.postDelayed(runnable, 500);
}
for (short[] buffer : mAudioData) {
// mPaint.setColor(Color.argb(brightness, 128, 255, 192));
float lastX = -1;
float lastY = -1;
// For efficiency, we don't draw all of the samples in the buffer, but only the ones
// that align with pixel boundaries.
for (int x = 0; x < width; x++) {
int index = (int) ((x / width) * buffer.length);
short sample = buffer[index];
float y = (sample / MAX_AMPLITUDE_TO_DRAW) * centerY + centerY;
if (lastX != -1) {
canvas.drawLine(lastX, lastY, x, y, mPaint);
}
lastX = x;
lastY = y;
}
brightness += colorDelta;
}
}
}
请...你能帮帮我,告诉我如何在这个gl表面视图上绘制水平居中线?