• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.samples.views
2 
3 import android.content.Context
4 import android.graphics.Color
5 import androidx.annotation.DrawableRes
6 import androidx.core.content.ContextCompat
7 import androidx.core.graphics.drawable.DrawableCompat
8 import android.util.AttributeSet
9 import android.view.View
10 import android.widget.ImageView
11 import android.widget.LinearLayout
12 import androidx.core.view.isVisible
13 import androidx.core.view.setPadding
14 import com.airbnb.lottie.samples.R
15 import com.airbnb.lottie.samples.getText
16 import kotlinx.android.synthetic.main.item_view_control_bar.view.*
17 
18 class ControlBarItemToggleView @JvmOverloads constructor(
19         context: Context,
20         attrs: AttributeSet? = null,
21         defStyleAttr: Int = 0
22 ) : LinearLayout(context, attrs, defStyleAttr) {
23 
24     init {
25         inflate(context, R.layout.item_view_control_bar, this)
26         orientation = HORIZONTAL
27         setBackgroundResource(R.drawable.control_bar_item_view_background)
28         setPadding(resources.getDimensionPixelSize(R.dimen.control_bar_button_padding))
<lambda>null29         attrs?.let {
30             val typedArray = context.obtainStyledAttributes(it, R.styleable.ControlBarItemToggleView, 0, 0)
31 
32             val textRes = typedArray.getResourceId(R.styleable.ControlBarItemToggleView_text, 0)
33             if (textRes != 0) {
34                 textView.text = getText(textRes)
35             }
36 
37             val drawableRes = typedArray.getResourceId(R.styleable.ControlBarItemToggleView_src, 0)
38             if (drawableRes == 0) {
39                 imageView.isVisible = false
40             } else {
41                 imageView.setImageResource(drawableRes)
42             }
43 
44             typedArray.recycle()
45         }
46     }
47 
childDrawableStateChangednull48     override fun childDrawableStateChanged(child: View) {
49         super.childDrawableStateChanged(child)
50         if (child is ImageView && child.drawable != null) {
51             val color =
52                     if (child.isActivated) Color.WHITE
53                     else ContextCompat.getColor(context, R.color.control_bar_content_unactivated)
54             DrawableCompat.setTint(child.drawable.mutate(), color)
55         }
56     }
57 
getTextnull58     fun getText() = textView.text.toString()
59 
60     fun setText(text: String) {
61         textView.text = text
62     }
63 
setImageResourcenull64     fun setImageResource(@DrawableRes drawableRes: Int) {
65         imageView.setImageResource(drawableRes)
66         childDrawableStateChanged(imageView)
67     }
68 }