Created
June 23, 2020 05:09
-
-
Save ktvipin27/5934a681067f77cd33621c14125e4f47 to your computer and use it in GitHub Desktop.
A custom view for displaying timer while recording video
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
class TimerView : LinearLayout { | |
private var startTime = 0L | |
private var timerHandler = Handler() | |
private val timerThread = object : Runnable { | |
override fun run() { | |
tvTimer.text = (SystemClock.uptimeMillis() - startTime).toDuration() | |
timerHandler.postDelayed(this, 0) | |
} | |
} | |
private val ivRedDot = ImageView(context).apply { | |
layoutParams = LayoutParams(6.px, 6.px).apply { | |
setImageResource(R.drawable.ic_red_dot_6dp) | |
setMargins(leftMargin, topMargin, 5.px, bottomMargin) | |
gravity = Gravity.CENTER | |
} | |
}.also { addView(it) } | |
private val tvTimer = TextView(context).apply { | |
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply { | |
gravity = Gravity.CENTER | |
setTextColor(Color.WHITE) | |
setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f) | |
text = "00:00" | |
setPaddingRelative(8, 5, 8, 5) | |
} | |
}.also { addView(it) } | |
constructor(context: Context) : this(context, null) | |
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( | |
context, | |
attrs, | |
defStyleAttr | |
) { | |
background = context.getDrawable(R.drawable.bg_rounded_corner_gray) | |
gravity = Gravity.CENTER | |
setPadding(5.px, 2.px, 5.px, 2.px) | |
} | |
fun startTimer() { | |
startTime = SystemClock.uptimeMillis() | |
timerHandler.postDelayed(timerThread, 0) | |
visibility = View.VISIBLE | |
ivRedDot.startBlinkAnimation() | |
} | |
fun stopTimer() { | |
timerHandler.removeCallbacks(timerThread) | |
ivRedDot.clearAnimation() | |
visibility = View.INVISIBLE | |
tvTimer.text = "00:00" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment