【问题标题】:ExoPlayer: how to change color of controller's elementsExoPlayer:如何更改控制器元素的颜色
【发布时间】:2019-01-30 01:59:09
【问题描述】:

我有一个通过exo_player_control_view.xml 文件引入的自定义控件。我需要的唯一区别是元素(按钮和时间线)的不同颜色。

但事实证明,我需要复制粘贴所有图标的 xml,仅更改颜色属性。这当然是可行的,但我希望有一种更简单的方法。

另一个问题是图标在 Apache 2.0 许可 (example) 下,我不确定是否允许将它们复制到我的项目中。

问题:如何改变控制器元素的颜色?是否可以在不复制和修改标准图标的情况下做到这一点?

【问题讨论】:

标签: android exoplayer exoplayer2.x


【解决方案1】:

实际上在这里使用着色做类似的事情。假设您的 exoplayer 布局 xml 文件包含以下内容

    <ImageButton android:id="@id/exo_prev"
        style="@style/ExoMediaButton.Previous"/>

    <ImageButton android:id="@id/exo_rew"
        style="@style/ExoMediaButton.Rewind"/>

    <ImageButton android:id="@id/exo_play"
        style="@style/ExoMediaButton.Play"/>

    <ImageButton android:id="@id/exo_pause"
        style="@style/ExoMediaButton.Pause"/>

    <ImageButton android:id="@id/exo_ffwd"
        style="@style/ExoMediaButton.FastForward"/>

然后你可以这样做:

    val playButton = audioView.findViewById<ImageButton>(R.id.exo_play)
    tintButton(playButton, color)
    val pauseButton = audioView.findViewById<ImageButton>(R.id.exo_pause)
    tintButton(pauseButton, color)
    val prevButton = audioView.findViewById<ImageButton>(R.id.exo_prev)
    tintButton(prevButton, color)
    val rewindButton = audioView.findViewById<ImageButton>(R.id.exo_rew)
    tintButton(rewindButton, color)
    val ffwdButton = audioView.findViewById<ImageButton>(R.id.exo_ffwd)
    tintButton(ffwdButton, color)

private fun tintButton(button: ImageButton, color: Int) {
    val drawable = DrawableCompat.wrap(button.drawable)
    DrawableCompat.setTintList(drawable.mutate(), ColorStateList.valueOf(color))
    button.setImageDrawable(drawable)
}

【讨论】:

  • 谢谢。我有这个想法。可能是我弄乱了一些东西,但它不是这样工作的。我从我的片段的onStart() 方法调用代码,我的片段是ViewPager 的一部分,每个片段都包含这样的控制器。我目前正在尝试找出问题所在。
【解决方案2】:

试试这个:

ImageButton play = findViewById(R.id.exo_play);
Drawable drawable = play.getDrawable();
drawable.setTintList(ColorStateList.valueOf(Color.parseColor("#41a8ff")));

【讨论】:

  • 谢谢 (+1)。有机会我会试试这个。
猜你喜欢
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
相关资源
最近更新 更多