android 音乐波浪线(music wave)

效果图:

music_wave_sample.gif

代码:

/**
 * Created by wangjie
 */

public class MusicWaveView extends View{

    private long DEFAULT_DELAY_DRAW_TIME = 200L;
    private int startPosition = 0;
    private int mWaveWidth, mWaveHeight;
    private Paint mPaint;

    public MusicWaveView(Context context) {
        super(context);
    }

    public MusicWaveView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.white));
        mPaint.setDither(true);//设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰
        mPaint.setStrokeWidth(2);

        if (drawHandler != null) {
            drawHandler.removeCallbacksAndMessages(null);
            drawHandler.sendEmptyMessageDelayed(0x23, DEFAULT_DELAY_DRAW_TIME);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mWaveWidth = getWidth() / 7; //整个宽度分为7份, 可以画4个条形
        mWaveHeight = getHeight();   // 波浪条形的高度

        startPosition += 30;  // 每次加30度

        if (startPosition >= 180) {
            startPosition = 10;
        }
        // 计算 条形的长 宽,及绘制的点
        drawRect(canvas, 0, calculateWaveHeight(startPosition), mWaveWidth, mWaveHeight);
        drawRect(canvas, mWaveWidth * 2, calculateWaveHeight(startPosition + 45), mWaveWidth * 3, mWaveHeight);
        drawRect(canvas, mWaveWidth * 4, calculateWaveHeight(startPosition + 90), mWaveWidth * 5, mWaveHeight);
        drawRect(canvas, mWaveWidth * 6, calculateWaveHeight(startPosition + 135), mWaveWidth * 7, mWaveHeight);
    }

    /**
     * 画矩形
     */
    private void drawRect(Canvas canvas, float left, float top, float right, float bottom) {
        canvas.drawRect(left, top, right, bottom, mPaint);
    }

    /**
     * 根据角度计算出高度
     * @param angle 角度
     * @return
     */
    private float calculateWaveHeight(int angle) {
        return (float) sin(angle) * mWaveHeight;
    }

    private static double sin(int i) {
        if (i > 180) {
            i = i - 180;
        }
        return Math.sin(i * Math.PI / 180);
    }

    private Handler drawHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x23:
                    invalidate();//通过更改count 值,在重新画贝塞尔曲线,已达到动态的效果
                    sendEmptyMessageDelayed(0x23, 200);
                    break;
            }
        }
    };

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            if (drawHandler != null) {
                drawHandler.removeCallbacksAndMessages(null);
                drawHandler.sendEmptyMessageDelayed(0x23, DEFAULT_DELAY_DRAW_TIME);
            }
        } else {
            if (drawHandler != null)
                drawHandler.removeCallbacksAndMessages(null);
        }
    }
}

(完)

标签: none

添加新评论