Real-time data visualization in Android

I am working on an audio VU meter, and I would like to get something like the image shown below with the amplitude of the sound displayed in real time.

I know how to get the amplitude of sound for each channel, but I'm not sure how to make color rectangles display fast enough.

My first idea is to have a Viewbackground color for each rectangle and show / hide it according to the amplitude.

Has anyone already tried this? Would it be fast enough to display the amplitude every 50 or 100 ms?

What is the fastest way to achieve this?

Thanks you

enter image description here

+3
source share
1 answer

, , ? . . onDraw(). onDraw canvas.drawRect() . 30-60 . , .

private class AmplitudeView extends View {

    private int mWidth;
    private int mHeight;
    private Paint mRectPaint;

    public AmplitudeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRectPaint = new Paint() {
            {
                setStyle(Style.FILL);
            }
        };
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int numRects = 10;
        int red = 255;
        int green = 0;
        int incr = 255/numRects;
        for (int i = 0; i< numRects;i++){
            //TODO calculate color based on amplitude
            mRectPaint.setColor(Color.argb(0xff, red, green, 0));
            //TODO calculate rectangle
            canvas.drawRect(r, mRectPaint);
            red-=incr;
            green+=incr;
        }
    }
}
+2

All Articles