Skip to content

Instantly share code, notes, and snippets.

@raghunandankavi2010
Created July 20, 2021 18:11
Show Gist options
  • Save raghunandankavi2010/c8bad1abf242a9874b9683697d21c24d to your computer and use it in GitHub Desktop.
Save raghunandankavi2010/c8bad1abf242a9874b9683697d21c24d to your computer and use it in GitHub Desktop.
TickBar.
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.core.content.ContextCompat
import com.peoplemesh.now.R
class TagTickBar(context: Context, attrs: AttributeSet?, defStyleAttr:Int) :AppCompatSeekBar(context,attrs,defStyleAttr){
constructor(context: Context) : this(context,null)
constructor(context: Context, attrs: AttributeSet?) : this(context,attrs, R.attr.seekBarStyle)
override fun draw(canvas: Canvas) {
super.draw(canvas)
val mTickMark = ContextCompat.getDrawable(context, R.drawable.my_tag_tick)
mTickMark?.let {
val count = 4
val w: Int = it.intrinsicWidth
val h: Int = it.intrinsicHeight
val halfW = if (w >= 0) w / 2 else 1
val halfH = if (h >= 0) h / 2 else 1
it.setBounds(-halfW, -halfH, halfW, halfH)
val spacing: Float = (width - paddingLeft
- paddingRight) / count.toFloat()
val saveCount = canvas.save()
canvas.translate(paddingLeft.toFloat()+spacing, height / 2.toFloat())
for (i in 0 until count-1) {
it.draw(canvas)
canvas.translate(spacing, 0f)
}
canvas.restoreToCount(saveCount)
}
if(progress > 0) drawBenchMark(canvas,progress)
}
private fun drawBenchMark(canvas: Canvas, progress: Int) {
val mTickMark = ContextCompat.getDrawable(context, R.drawable.my_tag_bench_mark)
mTickMark?.let {
val count = 4
val w: Int = it.intrinsicWidth
val h: Int = it.intrinsicHeight
val halfW = if (w >= 0) w / 2 else 1
val halfH = if (h >= 0) h / 2 else 1
it.setBounds(-halfW, -halfH, halfW, halfH)
val spacing: Float = (width - paddingLeft
- paddingRight) / count.toFloat()
val saveCount = canvas.save()
canvas.translate(paddingLeft.toFloat()+spacing, height / 2.toFloat())
for (i in 0 until count-1) {
if(progress <= 25 && i== 1) {
it.draw(canvas)
canvas.translate(spacing, 0f)
}
else if(progress in 26..50 && i== 1) {
it.draw(canvas)
canvas.translate(spacing, 0f)
}
else if(progress in 51..75 && i== 2) {
it.draw(canvas)
canvas.translate(spacing, 0f)
} else if(progress in 76..100 && i== 3) {
it.draw(canvas)
canvas.translate(spacing, 0f)
}
}
canvas.restoreToCount(saveCount)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment