Android实现音频条形图效果

本文实例为大家分享了Android实现音频条形图效果的具体代码,供大家参考,具体内容如下

效果图:

通过自定义View和属性动画实现此效果

public class BarChartView extends LinearLayout implements Runnable {
    private ViewWrapper[] mViewWrapper;

    private int barchartCount = 1;
    private int barchartWidth = 20;
    private int barchartHeight = 0;
    private int barcharMarginLeft = 5;
    private int barchartDuration = 500;
    private int barcharBackColor;

    private boolean startAnimtor = false;

    @DrawableRes
    private int myShape;

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

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

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

    /**
     * 初始化配置
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, @Nullable AttributeSet attrs) {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BarChartView);
        barchartCount = typedArray.getInt(R.styleable.BarChartView_barchartCount, 0);
        barchartWidth = typedArray.getDimensionPixelSize(R.styleable.BarChartView_barchartWidth, 0);
        barchartHeight = typedArray.getDimensionPixelSize(R.styleable.BarChartView_barchartHeight, 0);
        barcharMarginLeft = typedArray.getDimensionPixelSize(R.styleable.BarChartView_barcharMarginLeft, 0);
        barchartDuration = typedArray.getInt(R.styleable.BarChartView_barchartDuration, 500);
        myShape = typedArray.getResourceId(R.styleable.BarChartView_barchartShape, 0);
        barcharBackColor = typedArray.getColor(R.styleable.BarChartView_barcharBackColor, Color.RED);
        typedArray.recycle();
    }

    /**
     * add View
     */
    private void addBarView() {
        if (barchartCount <= 0) {
            return;
        }
        mViewWrapper = new ViewWrapper[barchartCount];
        ImageView childView;
        LinearLayout.LayoutParams layoutParams;
        ViewWrapper viewWrapper;
        for (int i = 0; i < barchartCount; i++) {
            childView = new ImageView(getContext());
            if (myShape != 0) {
                childView.setBackgroundResource(myShape);
            } else {
                childView.setBackgroundColor(barcharBackColor);
            }
            layoutParams = new LayoutParams(barchartWidth, 100);
            layoutParams.setMargins(barcharMarginLeft, 0, 0, 0);
            childView.setLayoutParams(layoutParams);
            addView(childView);
            viewWrapper = new ViewWrapper(childView);
            mViewWrapper[i] = viewWrapper;
        }
    }

    /**
     * 开始动画
     */
    public void start() {
        if (mViewWrapper == null || mViewWrapper.length <= 0) {
            return;
        }
        startAnimtor = true;
        Random a = new Random();
        for (int i = 0; i < mViewWrapper.length; i++) {
            startAnimator(mViewWrapper[i], a.nextInt(barchartHeight));
        }
        removeCallbacks(this);
        postDelayed(this, barchartDuration);
    }

    /**
     * 停止动画
     */
    public void stop() {
        startAnimtor = false;
        for (int i = 0; i < mViewWrapper.length; i++) {
            startAnimator(mViewWrapper[i], 1);
        }
    }

    private void startAnimator(ViewWrapper viewWrapper, int height) {
        viewWrapper.mTarget.clearAnimation();
        ObjectAnimator.ofInt(viewWrapper, "height", height).setDuration(barchartDuration).start();
    }

    @Override
    public void run() {
        if (startAnimtor) {
            start();
        }
    }
    
    private static class ViewWrapper {
        public View mTarget;

        public ViewWrapper(View target) {
            mTarget = target;
        }

        public int getWidth() {
            return mTarget.getLayoutParams().width;
        }

        public void setWidth(int width) {
            mTarget.getLayoutParams().width = width;
            mTarget.requestLayout();
        }

        public int getHeight() {
            return mTarget.getLayoutParams().height;
        }

        public void setHeight(int height) {
            mTarget.getLayoutParams().height = height;
            mTarget.requestLayout();
        }
    }
}

自定义属性

<declare-styleable name="BarChartView">
        <attr name="barchartCount" format="integer" />
        <attr name="barchartDuration" format="integer" />
        <attr name="barchartShape" format="reference" />
        <attr name="barchartHeight" format="dimension" />
        <attr name="barchartWidth" format="dimension" />
        <attr name="barcharMarginLeft" format="dimension" />
        <attr name="barcharBackColor" format="color" />
</declare-styleable>

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.tlkg.testdemo.valueanimatordemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <com.tlkg.testdemo.valueanimatordemo.BarChartView
            android:id="@+id/myRealMapView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:barcharMarginLeft="3dp"
            app:barchartCount="10"
            app:barchartDuration="500"
            app:barchartHeight="100dp"
            app:barchartWidth="10dp" />

        <com.tlkg.testdemo.valueanimatordemo.BarChartView
            android:id="@+id/myRealMapView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff000000"
            app:barcharBackColor="#ffffffff"
            app:barcharMarginLeft="3dp"
            app:barchartCount="10"
            app:barchartDuration="500"
            app:barchartHeight="70dp"
            app:barchartWidth="5dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <com.tlkg.testdemo.valueanimatordemo.BarChartView
            android:id="@+id/myRealMapView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:barcharMarginLeft="1dp"
            app:barchartCount="50"
            app:barchartDuration="300"
            app:barchartHeight="40dp"
            app:barcharBackColor="#ff0000ff"
            app:barchartWidth="2dp" />

        <com.tlkg.testdemo.valueanimatordemo.BarChartView
            android:id="@+id/myRealMapView3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:barcharMarginLeft="3dp"
            app:barchartCount="8"
            app:barchartDuration="200"
            app:barchartHeight="90dp"
            app:barchartShape="@drawable/mshape"
            app:barchartWidth="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stop" />
    </LinearLayout>
</LinearLayout>

MainActivity代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final BarChartView realMapView = (BarChartView) findViewById(R.id.myRealMapView);
        final BarChartView realMapView1 = (BarChartView) findViewById(R.id.myRealMapView1);
        final BarChartView realMapView2 = (BarChartView) findViewById(R.id.myRealMapView2);
        final BarChartView realMapView3 = (BarChartView) findViewById(R.id.myRealMapView3);

        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                realMapView.start();
                realMapView1.start();
                realMapView2.start();
                realMapView3.start();
            }
        });

        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                realMapView.stop();
                realMapView1.stop();
                realMapView2.stop();
                realMapView3.stop();
            }
        });
    }

}

实现原理,BarChartView继承线性布局,设置水平,内容底部居中,根据初始化后的barchartCount属性添加childView,调用方法start开始属性动画。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现音频条形图效果(仿音频动画无监听音频输入)

    音频条形图 如下图所示就是这次的音频条形图: 由于只是自定义View的用法,我们就不去真实地监听音频输入了,随机模拟一些数字即可. 如果要实现一个如上图的静态音频条形图,相信大家应该可以很快找到思路,也就是绘制一个个的矩形,每个矩形之间稍微偏移一点距离即可.如下代码就展示了一种计算坐标的方法. for (int i = 0; i < mRectCount; i++) { // 矩形的绘制是从左边开始到上.右.下边(左右边距离左边画布边界的距离,上下边距离上边画布边界的距离) canvas.dra

  • Android绘制简单条形图

    本文实例为大家分享了Android绘制简单条形图的具体代码,供大家参考,具体内容如下 一种方案是:path先添加一个圆角矩形,再添加一个角的正方形 另一种直接通过api ,radii 需要传入8个数值,分四组,从左上角开始 addRoundRect(RectF rect, float[] radii, Direction dir) 中间有个小点:如果在一个循环里,同时画柱子和文字,会出现前面3个柱子上的文字被柱子覆盖,只有最后1个柱子才展示文字标记. 比如说1是柱子,2是文字,画个柱子,画个文字

  • 基于Web Audio API实现音频可视化效果

    网页音频接口最有趣的特性之一它就是可以获取频率.波形和其它来自声源的数据,这些数据可以被用作音频可视化.这篇文章将解释如何做到可视化,并提供了一些基础使用案例. 基本概念节 要从你的音频源获取数据,你需要一个 AnalyserNode节点,它可以用 AudioContext.createAnalyser() 方法创建,比如: var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); var analyser

  • android实现音乐跳动效果的示例代码

    效果图 实现 整体的流程图如下 上面主要步骤分为3个 1.计算宽度能放下多少列的音频块. 2.计算每一列中音频块的个数 3.绘制音频块 1.计算宽度能放下多少列的音频块. 设置音频块的宽度为danceWidth,音频块横向之间的间距为danceGap,那么可以算出能放的列数: /** * 先计算当前宽度能够放下多少个音频块 */ val widthNum = (getAvailableWith() / (danceGap + danceWidth)).toInt() /** * 获取可以用的宽度

  • Android实现文字消除效果

    今天和大家分享一个如何从右到左消除文本的动画. 先看效果图: 由于项目和语音识别相关,有时候人在不经意间交流的无效音频会被识别出来,并展示于界面,为了美观,客户要求我们将这些无效的识别文本用一个从右到左的动画给清除,于是便有了下述的技术实现. 嗯,效果做完后发现原理及其简单,仅此记录一下. 1.layout文件先在这儿贴一下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and

  • 分享Android仿刮奖效果控件

    本文实例为大家分享了Android刮刮卡效果控件,供大家参考,具体内容如下 刮刮卡类: package com.reyo.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Paint; import android.gr

  • Android实现左右滑动效果的方法详解

    本示例演示在Android中实现图片左右滑动效果. 关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来就让我们开始实现这种效果.为了方便大家理解,我们先来看一下效果图:主要效果图如下图:    接下来我们看一下程序结构图: MainActivity文件中代码: 复制代码 代码如下: package com.android.flip;import android.app.Activity;import a

  • Android编程ViewPager回弹效果实例分析

    本文实例讲述了Android编程ViewPager回弹效果.分享给大家供大家参考,具体如下: 其实在我们很多应用中都看到当ViewPager滑到第一页或者最后一页的时候,如果再滑动的时候,就会有一个缓冲的过程,也就是回弹效果.之前在研究回弹效果的时候,也顺便实现了ViewPager的回弹效果,其实也很简单,一下是实现代码,注释比较少: package com.freesonfish.viewpager_2; import android.content.Context; import andro

  • Android实现跑马灯效果的方法

    本文实例讲述了Android实现跑马灯效果的方法.分享给大家供大家参考.具体如下: 运行效果截图如下: 直接在布局里写代码就好了: <TextView android:id="@+id/menu_desc" android:layout_width="300dip" android:layout_height="wrap_content" android:text="温馨提示:左右滑动更改菜单,点击进入" android

  • Android TextView 字体滚动效果

    Android TextView 字体滚动效果 实例代码: package com.godinsec.seland.ui.tools; import android.content.Context; import android.text.TextUtils.TruncateAt; import android.util.AttributeSet; import android.widget.TextView; public class MarqueTextView extends TextVi

  • Android编程实现抽屉效果的方法示例

    本文实例讲述了Android编程实现抽屉效果的方法.分享给大家供大家参考,具体如下: 今天在手机上实现了抽屉效果,其实很简单,但是效果却很酷. 首先在layout 下设置xml布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

随机推荐