Android實(shí)現(xiàn)音頻條形圖效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)音頻條形圖效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
通過自定義View和屬性動(dòng)畫實(shí)現(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; ? ? ? ? } ? ? } ? ? /** ? ? ?* 開始動(dòng)畫 ? ? ?*/ ? ? 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); ? ? } ? ? /** ? ? ?* 停止動(dòng)畫 ? ? ?*/ ? ? 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(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
實(shí)現(xiàn)原理,BarChartView繼承線性布局,設(shè)置水平,內(nèi)容底部居中,根據(jù)初始化后的barchartCount屬性添加childView,調(diào)用方法start開始屬性動(dòng)畫。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)中使用achartengine繪制各種圖表的方法
這篇文章主要介紹了Android開發(fā)中使用achartengine繪制各種圖表的方法,結(jié)合具體實(shí)例形式分析了Android基于圖表生成類庫achartengine進(jìn)行圖表繪制的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解
這篇文章主要為大家介紹了Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android8.1 通過黑名單屏蔽系統(tǒng)短信和來電功能
最近小編接到一個(gè)新的需求,需要將8.1 設(shè)備的來電功能和短信功能都屏蔽掉,特殊產(chǎn)品就是特殊定制。接下來通過本文給大家介紹Android8.1 通過黑名單屏蔽系統(tǒng)短信和來電功能,需要的朋友參考下吧2019-05-05Kotlin開發(fā)實(shí)戰(zhàn)之hello world
這篇文章主要為大家詳細(xì)介紹了Kotlin開發(fā)實(shí)戰(zhàn)之hello world的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android實(shí)現(xiàn)View拖拽跟隨手指移動(dòng)效果
這篇文章主要介紹了Android實(shí)現(xiàn)View拖拽跟隨手指移動(dòng)效果,主要使用setTranslationX() 和setTranslationY() 屬性方法實(shí)現(xiàn)的,需要的朋友參考下吧2017-08-08Android開場(chǎng)動(dòng)畫類完整實(shí)現(xiàn)代碼
這篇文章主要介紹了Android開場(chǎng)動(dòng)畫類完整實(shí)現(xiàn)代碼,是非常實(shí)用的功能,需要的朋友可以參考下2014-07-07Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android端實(shí)現(xiàn)單點(diǎn)登錄的方法詳解
所謂單點(diǎn)登錄就是指的同一個(gè)賬戶(id)不能在一個(gè)以上的設(shè)備上登錄對(duì)應(yīng)的用戶系統(tǒng)(排除web端和移動(dòng)端可以同時(shí)登錄的情況),例如:用戶m在A設(shè)備登錄并保持登錄狀態(tài),然后又在B設(shè)備登錄,此時(shí)A應(yīng)該要強(qiáng)制下線,m無法在A設(shè)備上繼續(xù)執(zhí)行用戶相關(guān)的操作,下面來一起看看吧。2016-11-11