自定义View-音频条形图

2018-09-10  本文已影响13人  吴_旭东

随机模拟音频条形图

音频条.gif

SoundView.java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.dadong.testjni.R;

import java.util.Random;

/**
 * Created by dadong on 2018/9/6.
 * Describe:
 */
public class SoundView extends View {

    private int soundviewCount;
    private int soundviewPadding;
    private int soundviewStartColor;
    private int soundviewEndColor;

    private int soundviewDuration;
    private int soundviewWidth;
    private int soundviewHeight;
    private Random mRandom = new Random();

    private Paint paint;

    public SoundView(Context context) {
        this(context, null);
    }

    public SoundView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SoundView);
        soundviewCount = typedArray.getInt(R.styleable.SoundView_soundviewCount, 5);
        soundviewStartColor = typedArray.getColor(R.styleable.SoundView_soundviewStartColor, context.getResources().getColor(R.color.colorPrimary));
        soundviewEndColor = typedArray.getColor(R.styleable.SoundView_soundviewEndColor, context.getResources().getColor(R.color.colorPrimaryDark));
        soundviewPadding = typedArray.getDimensionPixelSize(R.styleable.SoundView_soundviewPadding, 3);
        soundviewWidth = typedArray.getDimensionPixelOffset(R.styleable.SoundView_soundviewWidth, 5);
        soundviewHeight = typedArray.getDimensionPixelOffset(R.styleable.SoundView_soundviewHeight, 15);
        soundviewDuration = typedArray.getInt(R.styleable.SoundView_soundviewDuration, 300);
        typedArray.recycle();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(soundviewStartColor);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //计算view大小
        int height;
        int width;
        width = soundviewWidth * soundviewCount + soundviewPadding * (soundviewCount + 1);
        height = soundviewHeight + getPaddingTop() * 2;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制音频条纹
        for (int i = 0; i < soundviewCount; ++i) {
            int distance = mRandom.nextInt(soundviewHeight / 2);
            int startX = getPaddingLeft() + (soundviewWidth + soundviewPadding) * i;
            int startY = getPaddingTop() + soundviewHeight / 2 - distance;
            int stopX = getPaddingLeft() + (soundviewWidth + soundviewPadding) * i + soundviewWidth;
            int stopY = getPaddingTop() + soundviewHeight / 2 + distance;
            canvas.drawRect(startX, startY, stopX, stopY, paint);
        }
        //动画
        postInvalidateDelayed(soundviewDuration);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int mWidth = getWidth();
        int mRectHeight = getHeight();
        int mRectWidth = (int) (mWidth - soundviewPadding * (soundviewCount - 1)) / soundviewCount;
        //设置颜色渐变
        LinearGradient mLinearGradient = new LinearGradient(
                0,
                0,
                mRectWidth,
                mRectHeight,
                soundviewStartColor,
                soundviewEndColor,
                Shader.TileMode.CLAMP);
        paint.setShader(mLinearGradient);
    }

}

attrs.xml

 <declare-styleable name="SoundView">
        <attr name="soundviewStartColor" format="color" />
        <attr name="soundviewPadding" format="dimension" />
        <attr name="soundviewCount" format="integer" />
        <attr name="soundviewWidth" format="dimension" />
        <attr name="soundviewHeight" format="dimension" />
        <attr name="soundviewDuration" format="integer" />
        <attr name="soundviewEndColor" format="color" />
    </declare-styleable>
上一篇下一篇

猜你喜欢

热点阅读