Skip to content

Instantly share code, notes, and snippets.

@ktvipin27
Last active June 26, 2020 15:58
Show Gist options
  • Save ktvipin27/55aa0dc9eee65e5befbabf194c770745 to your computer and use it in GitHub Desktop.
Save ktvipin27/55aa0dc9eee65e5befbabf194c770745 to your computer and use it in GitHub Desktop.
A custom view for controlling camera
class ControlView : LinearLayout {
private var isLongPressed: Boolean = false
private var flashMode: FlashMode = FlashMode.FLASH_MODE_OFF
private var listener: Listener? = null
private val timerView = TimerView(context)
.apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
visibility = View.INVISIBLE
}.also { addView(it) }
private val layoutControls = LinearLayout(context).apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
orientation = HORIZONTAL
topMargin = 5.px
}
}.also { addView(it) }
private val ivFlash = ImageButton(context).apply {
layoutParams = LayoutParams(48.px, 48.px).apply {
gravity = Gravity.CENTER
}
setImageResource(R.drawable.ic_baseline_flash_off_24)
setBackgroundColor(Color.TRANSPARENT)
setOnClickListener { toggleFlash() }
}.also { layoutControls.addView(it) }
private val ivCapture = ImageButton(context).apply {
layoutParams = LayoutParams(70.px, 70.px).apply {
setMargins(70.px, 20.px, 70.px, 20.px)
gravity = Gravity.CENTER
}
setImageResource(R.drawable.ic_circle_line_white_70)
setBackgroundColor(Color.TRANSPARENT)
}.also {
isHapticFeedbackEnabled = true
layoutControls.addView(it)
}
private val ivSwitchCam = ImageButton(context).apply {
layoutParams = LayoutParams(48.px, 48.px).apply {
gravity = Gravity.CENTER
}
setImageResource(R.drawable.ic_baseline_flip_camera_24)
setBackgroundColor(Color.TRANSPARENT)
setOnClickListener {
it.startRotateAnimation()
listener?.toggleCamera()
}
}.also { layoutControls.addView(it) }
private val tvInfo = TextView(context).apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
gravity = Gravity.CENTER
topMargin = 5.px
}
setTextColor(Color.WHITE)
setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
text = "Hold for Video, tap for Photo"
}.also { addView(it) }
var flashVisibility:Boolean = true
set(value) {
field = value
ivFlash.visibility = if (value) View.VISIBLE else View.INVISIBLE
}
var cameraToggleVisibility:Boolean = true
set(value) {
field = value
ivSwitchCam.visibility = if (value) View.VISIBLE else View.INVISIBLE
}
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
) {
setPadding(16.px)
setBackgroundColor(Color.TRANSPARENT)
gravity = Gravity.CENTER
orientation = VERTICAL
}
fun setListener(listener: Listener?) {
this.listener = listener
}
fun getFlashMode() = flashMode
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment