Back to project page rfcx-client-stream-android.
The source code is released under:
Apache License
If you think the Android project rfcx-client-stream-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.rfcx.android.stream.view; //from w w w . j a v a 2 s .c om import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; /** * Created by Natthawut Hemathulin on 12/16/14 AD. * Email: natthawut1991@gmail.com */ public class VisualizerView extends View { private static final boolean ANTI_ALIAS = true; private static final float STROKE_WIDTH = 2f; private static final int WAVE_WIDTH = 4; private static final int STROKE_COLOR = Color.parseColor("#FFFFFF"); private byte[] mBytes; private float[] mPoints; private Rect mRect = new Rect(); private Paint mForePaint = new Paint(); public VisualizerView(Context context) { super(context); init(); } public VisualizerView(Context context, AttributeSet attributeSet) { super(context, attributeSet); init(); } private void init() { mBytes = null; mForePaint.setStrokeWidth(STROKE_WIDTH); // Stroke width mForePaint.setAntiAlias(ANTI_ALIAS); mForePaint.setColor(STROKE_COLOR); // Path color } public void updateVisualizer(byte[] bytes) { mBytes = bytes; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mBytes == null) { return; } if (mPoints == null || mPoints.length < mBytes.length * 4) { mPoints = new float[mBytes.length * 4]; } mRect.set(0, 0, getWidth(), getHeight()); for (int i = 0; i < mBytes.length - 1; i++) { mPoints[i * WAVE_WIDTH] = mRect.width() * i / (mBytes.length - 1); mPoints[i * WAVE_WIDTH + 1] = mRect.height() / 2 + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128; mPoints[i * WAVE_WIDTH + 2] = mRect.width() * (i + 1) / (mBytes.length - 1); mPoints[i * WAVE_WIDTH + 3] = mRect.height() / 2 + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128; } canvas.drawLines(mPoints, mForePaint); } }