前言
稍微讀一下Seekbar的源碼,瞭解一下具體實現。
正文
seekbar的父控件是ProgressBar,這個比較簡單,大概基本就是一個把一個drawable在ondraw中給draw一下,onmesure則基本上是根據maxheight或者minheight來確定大小。
無論是drawable或者是maxheight、minheight都是通過style中定義的,我們看下系統的一個style如下:
<style name="Widget.ProgressBar.Horizontal">
<item name="indeterminateOnly">false</item>
<item name="progressDrawable">@drawable/progress_horizontal</item>
<item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
<item name="minHeight">20dip</item>
<item name="maxHeight">20dip</item>
<item name="mirrorForRtl">true</item>
</style>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/progressbar_indeterminate1" android:duration="200" />
<item android:drawable="@drawable/progressbar_indeterminate2" android:duration="200" />
<item android:drawable="@drawable/progressbar_indeterminate3" android:duration="200" />
</animation-list>
關於onDraw其實就是直接調用一個drawable的ondraw方法,所有的邏輯基本都集中在一個drawlist,這個暫時不是很清楚具體實現,不過我們稍微關注一下progressBar的一些邏輯:
private synchronized void doRefreshProgress(int id, int progress, boolean fromUser,
setVisualProgress(id, scale);
}
private void setVisualProgress(int id, float progress) {
d.setLevel(level);
}
這個是用來更新drawable的狀態,稍微關注一下,主要邏輯就是這裏,對drawable設置一下level。
下面我們關注一下seekbar,他主要加入了拖拽功能,也就是加入ontauch的回調功能,這裏稍微分析一下。
onmessure功能就比較簡單,通過滑塊和Progressbar的高度寬度,功能控制大小。
onDraw就比較簡單,直接通過mThumbOffset 來畫一個滑塊。
關於intouch方法也很簡單,這裏就不詳細介紹,主要是調用setProgressInternal 來設置進度。