android之自定義音頻

public class VolumeView extends View {
    private int mWidth;
    private int mRectWidth;
    private int getmRectHeight;
    private Paint mPaint;
    private int mRectCount;
    private int offset=5;
    private double mRandom;
    private LinearGradient mLinearGradient;

    public VolumeView(Context context) {
        super(context);
        initView();
    }

    public VolumeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public VolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        mPaint=new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        mRectCount=12;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth=getWidth();
        getmRectHeight=getHeight();
        mRectWidth= (int) (mWidth*0.6/mRectCount);
        mLinearGradient=new LinearGradient(0,0,
                mRectWidth,
                getmRectHeight,
                Color.YELLOW,
                Color.BLUE,
                Shader.TileMode.CLAMP);
        mPaint.setShader(mLinearGradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i=0;i<mRectCount;i++){
            mRandom=Math.random();
            float currentHeight= (float) (getmRectHeight*mRandom);
            canvas.drawRect((float)(mWidth*0.4/2+mRectWidth*i+offset),
                    currentHeight,
                    (float)(mWidth*0.4/2+mRectWidth*(i+1)),
                    getmRectHeight,
                    mPaint);
        }
        postInvalidateDelayed(300);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章