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

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

    这里写图片描述

    源文件下载地址:源文件

     
     
  • 相关阅读:
    Codeforces-799C-Fountains(分类讨论+线段树)
    HDU-3486-Interviewe(二分+RMQ)
    小技巧---查doc文档的index.html怎么用的和chm一样
    chm文件右边部分查看不了
    最长公共临时文档7
    拓展欧几里得临时文档5
    关于myeclipse代码提示的一些问题
    mysql--乱码
    三分--Football Goal(面积最大)
    printf的一个常用技巧
  • 原文地址:https://www.cnblogs.com/lenkevin/p/5518969.html
Copyright © 2011-2022 走看看