Android 可拖動的seekbar自訂進度值_Android

來源:互聯網
上載者:User

最近接了個項目其中有需要要實現此功能:seekbar需要顯示最左和最右值,進度要跟隨進度塊移動。下面通過此圖給大家展示下效果,可能比文字描述要更清晰。

其實實現起來很簡單,主要是思路。自訂控制項的話也不難,之前我的部落格也有專門介紹,這裡就不再多說。

實現方案

這裡是通過繼承seekbar來自訂控制項,這樣的方式最快。主要痛點在於進度的顯示,其實我很的是最笨的方法,就是用了一個popwindow顯示在進度條的上方,然後在移動滑塊的時候即時的改變它顯示的橫座標。看進度顯示的核心代碼:

private void initHintPopup(){ String popupText = null;if (mProgressChangeListener!=null){popupText = mProgressChangeListener.onHintTextChanged(this, cuclaProcess(leftText));}LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);final View undoView = inflater.inflate(R.layout.popup, null);mPopupTextView = (TextView)undoView.findViewById(R.id.text);mPopupTextView.setText(popupText!=null? popupText : String.valueOf(cuclaProcess(leftText)));// mPopup.dismiss();if(mPopup == null)mPopup = new PopupWindow(undoView, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, false);else{mPopup.dismiss();mPopup = new PopupWindow(undoView, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, false);}}

布局很簡單,就一個TextView。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:background="#0fff"android:gravity="center"><TextView android:id="@+id/text"android:padding="8dp"android:textSize="16sp"android:singleLine="true"android:ellipsize="end"android:textColor="@color/green"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>

左右的顯示值原理也是一樣的,看以下代碼:

private void initRightText(){LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);final View undoView = inflater.inflate(R.layout.rightpop, null);mPopupRightView = (TextView)undoView.findViewById(R.id.righttext);mPopupRightView.setText(rightText+"");mRightPopup = new PopupWindow(undoView, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, false);mRightPopup.setAnimationStyle(R.style.fade_animation);}

那麼如何讓滑塊上方的文字跟著滑動。只要重寫onProgressChanged就可以了。

public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {String popupText = null;if (mProgressChangeListener!=null){popupText = mProgressChangeListener.onHintTextChanged(this, cuclaProcess(leftText));}if(mExternalListener !=null){mExternalListener.onProgressChanged(seekBar, progress, b);}step = cuclaProcess(leftText);mPopupTextView.setText(popupText!=null? popupText : String.valueOf(step));if(mPopupStyle==POPUP_FOLLOW){mPopup.update((int) (this.getX()+(int) getXPosition(seekBar)), (int) (this.getY()+2*mYLocationOffset+this.getHeight()), -1, -1);}}

其實最主要的就是算出x的位置getXPosition。看以上代碼:

private float getXPosition(SeekBar seekBar){float val = (((float)seekBar.getProgress() * (float)(seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax());float offset = seekBar.getThumbOffset()*2;int textWidth = mPopupWidth;float textCenter = (textWidth/2.0f);float newX = val+offset - textCenter;return newX;}

通過getProgress獲得進度來計算x移動的距離。這樣就實現了文字的移動。最後會給出源碼下載。

如何使用呢,跟普通自訂的控制項一樣,如下:

<com.canmou.cm4restaurant.tools.SeekBarHintandroid:id="@+id/seekbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_marginTop="40dp"android:progress="5"hint:popupWidth="40dp"hint:yOffset="10dp"hint:popupStyle="fixed"/>

當然目前實現了原生的樣式,下面來說說如何自訂seekbar的樣式。

自訂樣式

seekbar要改樣式得準備三張圖片,左邊己選擇的滑動條圖片,右邊未選擇的滑動條圖片和滑塊圖片,滑動條要9.png格式的最好。這裡為方便,直接用layer-list來處理滑動條部分。在drawable中定義xml檔案。

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:id="@android:id/background"><shape ><corners android:radius="10dip" /><gradientandroid:angle="180"android:centerColor="#F5F5F5"android:centerY="0.2"android:endColor="#F5F5F5"android:startColor="#F5F5F5" /></shape></item><item android:id="@android:id/progress"><clip ><shape ><corners android:radius="10dip" /><gradientandroid:angle="180"android:centerColor="#39ac69"android:centerY="0.45"android:endColor="#39ac69"android:startColor="#39ac69" /></shape></clip></item></layer-list>

這樣就實現了重疊的圖片。設定滑塊的圖片則直接在seekhint中設定:

android:thumb="@drawable/bt_seekbar"

到此進度值可拖動的seekbar就實現了。大家都看明白了,有任何疑問歡迎給雲棲社區小編留言,小編會及時給大家回複的。欲瞭解更多精彩內容請持續關注云棲社區網站,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.