最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)音頻條形圖效果

 更新時間:2022年08月17日 15:33:56   作者:這個殺手不太累  
這篇文章主要為大家詳細介紹了Android實現(xiàn)音頻條形圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)音頻條形圖效果的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

通過自定義View和屬性動畫實現(xiàn)此效果

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();
? ? ? ? ? ? }
? ? ? ? });
? ? }

}

實現(xiàn)原理,BarChartView繼承線性布局,設(shè)置水平,內(nèi)容底部居中,根據(jù)初始化后的barchartCount屬性添加childView,調(diào)用方法start開始屬性動畫。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義View實現(xiàn)簡單文字描邊功能

    Android自定義View實現(xiàn)簡單文字描邊功能

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)簡單文字描邊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android如何使用ViewPager2實現(xiàn)頁面滑動切換效果

    Android如何使用ViewPager2實現(xiàn)頁面滑動切換效果

    這篇文章主要給大家介紹了關(guān)于Android如何使用ViewPager2實現(xiàn)頁面滑動切換效果的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • Android 背景透明度設(shè)置總結(jié)

    Android 背景透明度設(shè)置總結(jié)

    這篇文章主要介紹了Android 背景透明度設(shè)置總結(jié),本文通過實例代碼給大家介紹的非常詳細,感興趣的的朋友參考下吧
    2017-06-06
  • Android實戰(zhàn)打飛機游戲之實現(xiàn)主角以及主角相關(guān)元素(3)

    Android實戰(zhàn)打飛機游戲之實現(xiàn)主角以及主角相關(guān)元素(3)

    這篇文章主要為大家詳細介紹了Android實戰(zhàn)打飛機游戲之實現(xiàn)主角以及主角相關(guān)元素,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Flutter路由之fluro的配置及跳轉(zhuǎn)

    Flutter路由之fluro的配置及跳轉(zhuǎn)

    本文主要介紹了Flutter路由之fluro的配置及跳轉(zhuǎn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android下通過httpClient發(fā)送GET和POST請求的實例代碼

    Android下通過httpClient發(fā)送GET和POST請求的實例代碼

    這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請求的實例代碼,有需要的朋友可以參考一下
    2013-08-08
  • Android超實用的Toast提示框優(yōu)化分享

    Android超實用的Toast提示框優(yōu)化分享

    Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。那么這篇文章跟大家分享下Android中Toast的優(yōu)化,對大家日常開發(fā)還是很實用,下面來一起看看吧。
    2016-09-09
  • Android可自定義神奇動效的卡片切換視圖實例

    Android可自定義神奇動效的卡片切換視圖實例

    今天小編就為大家分享一篇關(guān)于Android可自定義神奇動效的卡片切換視圖實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Android?使用flow實現(xiàn)倒計時的方式

    Android?使用flow實現(xiàn)倒計時的方式

    這篇文章主要介紹了Android?使用flow實現(xiàn)倒計時的方式,借助Flow這個工具,更加優(yōu)雅地實現(xiàn)這個需求功能,文末給大家整理了Android?實現(xiàn)倒計時的幾種方式,需要的朋友可以參考下
    2022-04-04
  • Kotlin Service實現(xiàn)消息推送通知過程

    Kotlin Service實現(xiàn)消息推送通知過程

    這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復(fù)雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口
    2022-12-12

最新評論

翁源县| 年辖:市辖区| 平遥县| 化州市| 家居| 盱眙县| 长岭县| 台前县| 泰宁县| 赤城县| 宣威市| 灵川县| 闽侯县| 关岭| 冕宁县| 衢州市| 洱源县| 沾化县| 洮南市| 盐边县| 高邮市| 镇宁| 昆明市| 博兴县| 六枝特区| 太和县| 安泽县| 古丈县| 石嘴山市| 池州市| 贵阳市| 乐东| 东海县| 光山县| 玉溪市| 固阳县| 瑞安市| 岳阳市| 凤庆县| 和田县| 南宫市|