欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 可拖動的seekbar自定義進度值

 更新時間:2016年04月19日 10:51:29   作者:showCar  
這篇文章主要介紹了Android 可拖動的seekbar自定義進度值的相關(guān)資料,有需要的朋友參考下

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

這里寫圖片描述

其實實現(xiàn)起來很簡單,主要是思路。自定義控件的話也不難,之前我的博客也有專門介紹,這里就不再多說。

實現(xiàn)方案

這里是通過繼承seekbar來自定義控件,這樣的方式最快。主要難點在于進度的顯示,其實我很的是最笨的方法,就是用了一個popwindow顯示在進度條的上方,然后在移動滑塊的時候?qū)崟r的改變它顯示的橫坐標(biāo)??催M度顯示的核心代碼:

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??匆陨洗a:

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移動的距離。這樣就實現(xiàn)了文字的移動。最后會給出源碼下載。

如何使用呢,跟普通自定義的控件一樣,如下:

<com.canmou.cm4restaurant.tools.SeekBarHint
android: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"/>

當(dāng)然目前實現(xiàn)了原生的樣式,下面來說說如何自定義seekbar的樣式。

自定義樣式

seekbar要改樣式得準(zhǔn)備三張圖片,左邊己選擇的滑動條圖片,右邊未選擇的滑動條圖片和滑塊圖片,滑動條要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" />
<gradient
android: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" 
/>
<gradient
android:angle="180"
android:centerColor="#39ac69"
android:centerY="0.45"
android:endColor="#39ac69"
android:startColor="#39ac69" />
</shape>
</clip>
</item>
</layer-list>

這樣就實現(xiàn)了重疊的圖片。設(shè)置滑塊的圖片則直接在seekhint中設(shè)置:

android:thumb="@drawable/bt_seekbar"

到此進度值可拖動的seekbar就實現(xiàn)了。大家都看明白了,有任何疑問歡迎給腳本之家小編留言,小編會及時給大家回復(fù)的。欲了解更多精彩內(nèi)容請持續(xù)關(guān)注腳本之家網(wǎng)站,謝謝!

相關(guān)文章

  • Android Studio3.6中的View Binding初探及用法區(qū)別

    Android Studio3.6中的View Binding初探及用法區(qū)別

    這篇文章主要介紹了Android 中的View Binding初探及用法區(qū)別,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android滾動條廣告實現(xiàn)代碼示例

    Android滾動條廣告實現(xiàn)代碼示例

    本篇文章主要介紹了Android滾動條廣告實現(xiàn)代碼示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Android開發(fā)之線程通信詳解

    Android開發(fā)之線程通信詳解

    這篇文章主要為大家詳細介紹了Android開發(fā)中線程間通信的相關(guān)資料,文中的示例代碼講解詳細,對我們學(xué)習(xí)Android有一定的幫助,?需要的可以了解一下
    2022-11-11
  • Android中制作自定義dialog對話框的實例分享

    Android中制作自定義dialog對話框的實例分享

    這篇文章主要介紹了Android中制作自定義dialog對話框的實例分享,安卓自帶的Dialog顯然不夠用,因而我們要繼承Dialog類來制作自己的對話框,需要的朋友可以參考下
    2016-04-04
  • Kotlin的Collection與Sequence操作異同點詳解

    Kotlin的Collection與Sequence操作異同點詳解

    這篇文章主要介紹了Kotlin的Collection與Sequence操作異同點詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • android隱式意圖激活瀏覽器的實現(xiàn)方法

    android隱式意圖激活瀏覽器的實現(xiàn)方法

    下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活瀏覽器的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Android開心消消樂代碼實例詳解

    Android開心消消樂代碼實例詳解

    這篇文章主要介紹了Android開心消消樂代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法

    RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法

    這篇文章主要為大家詳細介紹了RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android自定義View用切圖顯示字符串

    Android自定義View用切圖顯示字符串

    這篇文章主要為大家詳細介紹了Android自定義View用切圖顯示字符串,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android studio 引用aar 進行java開發(fā)的操作步驟

    Android studio 引用aar 進行java開發(fā)的操作步驟

    這篇文章主要介紹了Android studio 引用aar 進行java開發(fā)的操作步驟,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09

最新評論