Android 音乐柱状频谱控件

来源:互联网 发布:p2pipcamera软件下载 编辑:程序博客网 时间:2024/05/17 03:54

这里先给个效果图,注意他是跳动的,由于电脑现在没法制作GIF


这里先自定义一个控件:

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;import android.view.ViewGroup;/** * Created by shaolin on 6/22/16. */public class VisualizerView extends View {    private byte[] mBytes;    private float[] mPoints;    private Rect mRect = new Rect();    private Paint mForePaint = new Paint();    private int mSpectrumNum = 5;    private boolean mFirst = true;    public VisualizerView(Context context) {        this(context, null);    }    public VisualizerView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    private void init() {        mBytes = null;        mForePaint.setStrokeWidth(8f);        mForePaint.setAntiAlias(true);        mForePaint.setColor(Color.rgb(0, 128, 255));    }    public void updateVisualizer(byte[] fft) {        if (mFirst) {            mFirst = false;        }        byte[] model = new byte[fft.length / 2 + 1];        model[0] = (byte) Math.abs(fft[0]);        for (int i = 2, j = 1; j < mSpectrumNum; ) {            model[j] = (byte) Math.hypot(fft[i], fft[i + 1]);            i += 2;            j++;        }        mBytes = model;        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];        }        ViewGroup.LayoutParams params = getLayoutParams();        mRect.set(0, 0, params.width, params.height);        //绘制波形        // for (int i = 0; i < mBytes.length - 1; i++) {        // mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);        // mPoints[i * 4 + 1] = mRect.height() / 2        // + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;        // mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);        // mPoints[i * 4 + 3] = mRect.height() / 2        // + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;        // }        //绘制频谱        final int baseX = mRect.width() / mSpectrumNum;        final int height = mRect.height();        for (int i = 0; i < mSpectrumNum; i++) {            if (mBytes[i] < 0) {                mBytes[i] = 127;            }            final int xi = baseX * i + baseX / 2;            mPoints[i * 4] = xi;            mPoints[i * 4 + 1] = height;            mPoints[i * 4 + 2] = xi;            mPoints[i * 4 + 3] = height - mBytes[i];        }        canvas.drawLines(mPoints, mForePaint);    }}
例子MainActivity.java

import android.os.Bundle;import android.os.Handler;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends BaseActivity {    private VisualizerView mVisualizerView;    private Timer mTimer;    private TimerTask mTimerTask;    private Handler mHandler;    private byte[] getByte() {        byte[] datas = new byte[10];        for (int i = 0; i < datas.length; i++) {            datas[i] = (byte) (Math.random() * datas.length * 2);        }        return datas;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mVisualizerView = (VisualizerView) findViewById(R.id.visualizerView);        mHandler = new Handler();        mTimer = new Timer();        mTimerTask = new TimerTask() {            @Override            public void run() {                mHandler.post(new Runnable() {                    @Override                    public void run() {                        mVisualizerView.updateVisualizer(getByte());                    }                });            }        };        mTimer.schedule(mTimerTask, 1000, 100);    }}
layout_main.xml

 <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:padding="10dp">        <sourceforge.titlelan.VisualizerView            android:id="@+id/visualizerView"            android:layout_width="40dp"            android:layout_height="40dp" />    </LinearLayout>
借鉴:http://blog.csdn.net/gigatron/article/details/7866910

0 0
原创粉丝点击