zoukankan      html  css  js  c++  java
  • Android自定义View之音频条形图

     分类:
     

    新建项目,新建MusicRectangleView继承自View并重写onDrawonSizeChanged方法,onDraw方法用于绘制矩形onSizeChanged主要用于为矩形添加LinearGradient渐变,完整后新建变量如下:

    private Paint mPaint;
        public int mOffset = 10;
        public int mRectWidth ;
        public int mRectHeight ;
        private int mRectCount = 10;
        private float currentHeight;
        private int mWidth;
        private LinearGradient mLinearGradient;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    修改onDraw方法如下:

    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            for (int i=0;i<mRectCount;i++){
                currentHeight = (float)(Math.random()*mRectHeight);
                canvas.drawRect((float)(mWidth*0.4/2+mRectWidth*i+mOffset),currentHeight,(float)(mWidth*0.4/2+mRectWidth*(i+1)),mRectHeight,mPaint);
                postInvalidateDelayed(300);
            }
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    修改onSizeChanged方法如下:

    @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mPaint = new Paint();
            mWidth = getWidth();
            mRectHeight = getHeight();
            mRectWidth = (int)(mWidth*0.6/mRectCount);
            mLinearGradient = new LinearGradient(0,0,mRectWidth,mRectHeight, Color.YELLOW,Color.BLUE, Shader.TileMode.CLAMP);
            mPaint.setShader(mLinearGradient);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    自此就完成了一个音频条形图的绘制,效果如下:

    这里写图片描述

    源文件下载地址:源文件

     
     
  • 相关阅读:
    Kaggle 神器 xgboost
    改善代码可测性的若干技巧
    IDEA 代码生成插件 CodeMaker
    Elasticsearch 使用中文分词
    Java性能调优的11个实用技巧
    Lucene 快速入门
    Java中一个字符用unicode编码为什么不是两字节
    lucene 的评分机制
    面向对象设计的 10 条戒律
    2019.10.23-最长全1串(双指针)
  • 原文地址:https://www.cnblogs.com/lenkevin/p/5518969.html
Copyright © 2011-2022 走看看