Created
October 22, 2021 05:46
-
-
Save devrath/87a8b39bd85893dbb5d16f581a64b96d to your computer and use it in GitHub Desktop.
SeekBarImpl1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.appcompat.widget.LinearLayoutCompat 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:gravity="center" | |
android:background="@color/screen_background" | |
tools:context=".segmentedSeekBar.MainActivity"> | |
<com.example.code.percentSeekBar.PercentSeekBar | |
android:id="@+id/percentSeekBar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:progress="50" | |
android:thumbOffset="0dp" | |
android:paddingStart="0dp" | |
android:paddingEnd="0dp" | |
android:progressBackgroundTint="@color/gb_seek_bar_unplayed" | |
android:progressTint="@color/gb_seek_bar_played" | |
android:secondaryProgressTint="@color/gb_seek_bar_buffer" | |
android:thumbTint="@color/gb_seek_bar_played" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.appcompat.widget.LinearLayoutCompat> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.code.percentSeekBar | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import com.example.code.R | |
import com.example.code.percentSeekBar.PercentSeekBar | |
import com.example.code.percentSeekBar.ProgressItem | |
import java.util.ArrayList | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val progressBar: PercentSeekBar = findViewById(R.id.percentSeekBar) | |
progressBar.initData(prepareSections()) | |
Log.d("",prepareSections().toString()) | |
} | |
private fun prepareSections(): ArrayList<ProgressItem> { | |
val sectionOneVal = 20F | |
val sectionTwoVal = 20F | |
val sectionThreeVal = 40F | |
val sectionFourVal = 10F | |
val sectionDividerVal = 2F | |
val numberOfSections = 4 | |
val numberOfDividers = (numberOfSections-1)// (TotalBlocks-1) | |
val totalSpan = (sectionOneVal + sectionTwoVal + sectionThreeVal + sectionFourVal) + (numberOfDividers*sectionDividerVal) | |
val segOneRange = getRangeInPercentage(input = sectionOneVal,max=totalSpan) | |
val segTwoRange = getRangeInPercentage(input = sectionTwoVal,max=totalSpan) | |
val segThreeRange = getRangeInPercentage(input = sectionThreeVal,max=totalSpan) | |
val segFourRange = getRangeInPercentage(input = sectionFourVal,max=totalSpan) | |
val segDividerRange = getRangeInPercentage(input = sectionDividerVal,max=totalSpan) | |
return arrayListOf( | |
ProgressItem(segOneRange,R.color.red,false), | |
ProgressItem(segDividerRange,R.color.screen_background,true), | |
ProgressItem(segTwoRange,R.color.blue,false), | |
ProgressItem(segDividerRange,R.color.screen_background,true), | |
ProgressItem(segThreeRange,R.color.green,false), | |
ProgressItem(segDividerRange,R.color.screen_background,true), | |
ProgressItem(segFourRange,R.color.yellow,false) | |
) | |
} | |
private fun getRangeInPercentage(input:Float,max:Float): Float { | |
return input/max*100 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.code.percentSeekBar | |
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Paint | |
import android.graphics.Rect | |
import android.util.AttributeSet | |
import androidx.appcompat.widget.AppCompatSeekBar | |
import androidx.core.content.ContextCompat | |
import java.util.ArrayList | |
import kotlin.jvm.Synchronized | |
class PercentSeekBar : AppCompatSeekBar { | |
private var mProgressItemsList: ArrayList<ProgressItem>? = null | |
constructor(context: Context?) : super(context!!) | |
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) | |
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context!!, attrs, defStyle) | |
fun initData(progressItemsList: ArrayList<ProgressItem>) { | |
mProgressItemsList = progressItemsList | |
} | |
@Synchronized | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec) | |
} | |
override fun onDraw(canvas: Canvas) { | |
mProgressItemsList?.let { progressList -> | |
if (progressList.size > 0) { | |
val seekBarWidth = width | |
val seekBarHeight = height | |
val thumboffset = thumbOffset | |
var lastProgressX = 0 | |
var progressItemRight: Int | |
for (i in progressList.indices) { | |
val progressItem = progressList[i] | |
val progressItemWidth = currentProgressWidth(progressItem, seekBarWidth) | |
val progressPaint = currentProgressPaint(progressItem.color) | |
progressItemRight = lastProgressX + progressItemWidth | |
// for last item give right to progress item to the width | |
if (i == progressList.size - 1 | |
&& progressItemRight != seekBarWidth | |
) { | |
progressItemRight = seekBarWidth | |
} | |
drawProgress(lastProgressX, thumboffset, progressItemRight, seekBarHeight, | |
canvas, progressPaint) | |
lastProgressX = progressItemRight | |
} | |
super.onDraw(canvas) | |
} | |
} | |
} | |
private fun drawProgress(lastProgressX: Int, thumboffset: Int, progressItemRight: Int, | |
seekBarHeight: Int, canvas: Canvas, progressPaint: Paint | |
) { | |
val progressRect = Rect() | |
progressRect[lastProgressX, thumboffset / 2, progressItemRight] = seekBarHeight - thumboffset / 2 | |
canvas.drawRect(progressRect, progressPaint) | |
} | |
private fun currentProgressWidth( | |
progressItem: ProgressItem, | |
seekBarWidth: Int | |
) = (progressItem.progressItemPercentage * seekBarWidth / 100).toInt() | |
private fun currentProgressPaint(color: Int): Paint { | |
val currentPaint = Paint() | |
currentPaint.strokeWidth | |
currentPaint.color= ContextCompat.getColor(context, color) | |
return currentPaint | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.code.percentSeekBar | |
data class ProgressItem( | |
var progressItemPercentage: Float = 0f, | |
var color: Int = 0, | |
var isDivider: Boolean = false | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment