Skip to content

Commit

Permalink
Merge pull request #17 from yoadey/feature/maxProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
massoudss authored Feb 11, 2021
2 parents 3f1b2b2 + 5264fb5 commit 4dbcadd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ just add ``WaveformSeekBar`` in your java/kotlin code or xml.
```
<com.masoudss.lib.WaveformSeekBar
app:wave_progress="33"
app:wave_max_progress="100"
app:wave_width="5dp"
app:wave_gap="2dp"
app:wave_min_height="5dp"
Expand All @@ -55,6 +56,7 @@ just add ``WaveformSeekBar`` in your java/kotlin code or xml.
```
val waveformSeekBar = WaveformSeekBar(yourContext)
waveformSeekBar.progress = 33
waveformSeekBar.maxProgress = 100
waveformSeekBar.waveWidth = Utils.dp(this,5)
waveformSeekBar.waveGap = Utils.dp(this,2)
waveformSeekBar.waveMinHeight = Utils.dp(this,5)
Expand All @@ -70,6 +72,7 @@ waveformSeekBar.setSampleFrom(AUDIO_FILE || AUDIO_PATH)
```
WaveformSeekBar waveformSeekBar = new WaveformSeekBar(yourContext);
waveformSeekBar.setProgress(33);
waveformSeekBar.setMaxProgress(100);
waveformSeekBar.setWaveWidth(Utils.dp(this,5));
waveformSeekBar.setWaveGap(Utils.dp(this,2));
waveformSeekBar.setWaveMinHeight(Utils.dp(this,5));
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/masoudss/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ class MainActivity : AppCompatActivity() {
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})

waveMaxProgress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
waveProgress.max = progress
waveformSeekBar.maxProgress = progress.toFloat()
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {}

override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})

gravityRadioGroup.setOnCheckedChangeListener { _, checkedId ->

val radioButton = gravityRadioGroup.findViewById(checkedId) as RadioButton
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

<com.masoudss.lib.WaveformSeekBar
app:wave_progress="33"
app:wave_max_progress="100"
app:wave_width="5dp"
app:wave_gap="2dp"
app:wave_min_height="5dp"
Expand All @@ -88,6 +89,7 @@
android:text="Wave Width"
android:layout_gravity="center"/>
<SeekBar
android:min="1"
android:max="100"
android:progress="50"
android:id="@+id/waveWidth"
Expand Down Expand Up @@ -138,6 +140,20 @@
android:layout_width="300dp"
android:layout_height="wrap_content"/>

<TextView
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maximum Progress"
android:layout_gravity="center"/>
<SeekBar
android:min="1"
android:max="200"
android:progress="100"
android:id="@+id/waveMaxProgress"
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"/>

<LinearLayout
android:layout_marginTop="16dp"
Expand Down
15 changes: 11 additions & 4 deletions lib/src/main/java/com/masoudss/lib/WaveformSeekBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WaveformSeekBar : View {
waveBackgroundColor = ta.getColor(R.styleable.WaveformSeekBar_wave_background_color,waveBackgroundColor)
waveProgressColor = ta.getColor(R.styleable.WaveformSeekBar_wave_progress_color,waveProgressColor)
progress = ta.getInteger(R.styleable.WaveformSeekBar_wave_progress,progress)
maxProgress = ta.getFloat(R.styleable.WaveformSeekBar_wave_max_progress,maxProgress)
val gravity = ta.getString(R.styleable.WaveformSeekBar_wave_gravity)
waveGravity = when(gravity){
"1" -> WaveGravity.TOP
Expand Down Expand Up @@ -95,15 +96,15 @@ class WaveformSeekBar : View {
mWaveRect.set(lastWaveRight, top, lastWaveRight+waveWidth, top + waveHeight)

when {
mWaveRect.contains(getAvailableWith()*progress/100F, mWaveRect.centerY()) -> {
mWaveRect.contains(getAvailableWith()*progress/maxProgress, mWaveRect.centerY()) -> {
var bitHeight = mWaveRect.height().toInt()
if (bitHeight <= 0)
bitHeight = waveWidth.toInt()

val bitmap = Bitmap.createBitmap(getAvailableWith(),bitHeight , Bitmap.Config.ARGB_8888)
mProgressCanvas.setBitmap(bitmap)

val fillWidth = (getAvailableWith()*progress/100F)
val fillWidth = (getAvailableWith()*progress/maxProgress)

mWavePaint.color = waveProgressColor
mProgressCanvas.drawRect(0F,0F,fillWidth,mWaveRect.bottom,mWavePaint)
Expand All @@ -114,7 +115,7 @@ class WaveformSeekBar : View {
val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
mWavePaint.shader = shader
}
mWaveRect.right <= getAvailableWith()*progress/100F -> {
mWaveRect.right <= getAvailableWith()*progress/maxProgress -> {
mWavePaint.color = waveProgressColor
mWavePaint.shader = null
}
Expand Down Expand Up @@ -181,7 +182,7 @@ class WaveformSeekBar : View {

private fun updateProgress(event: MotionEvent?){

progress = (100*event!!.x/getAvailableWith()).toInt()
progress = (maxProgress*event!!.x/getAvailableWith()).toInt()
invalidate()

if (onProgressChanged != null)
Expand Down Expand Up @@ -213,6 +214,12 @@ class WaveformSeekBar : View {
onProgressChanged!!.onProgressChanged(this,progress,false)
}

var maxProgress : Float = 100F
set(value) {
field = value
invalidate()
}

var waveBackgroundColor : Int = Color.LTGRAY
set(value) {
field = value
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<declare-styleable name="WaveformSeekBar">
<attr name="wave_progress" format="integer"/>
<attr name="wave_max_progress" format="float"/>
<attr name="wave_width" format="dimension"/>
<attr name="wave_gap" format="dimension"/>
<attr name="wave_min_height" format="dimension"/>
Expand Down

0 comments on commit 4dbcadd

Please sign in to comment.