【发布时间】:2010-01-11 18:37:18
【问题描述】:
有没有办法自定义 MediaController?我需要更改按钮、SeekBar 等的样式。
【问题讨论】:
-
这是一个 MediaController 自定义示例:stackoverflow.com/questions/12482203/…
有没有办法自定义 MediaController?我需要更改按钮、SeekBar 等的样式。
【问题讨论】:
您可以做的是递归 MediaController 的视图层次结构并以编程方式设置 SeekBar 的可绘制对象:
private void styleMediaController(View view) {
if (view instanceof MediaController) {
MediaController v = (MediaController) view;
for(int i = 0; i < v.getChildCount(); i++) {
styleMediaController(v.getChildAt(i));
}
} else
if (view instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) view;
for(int i = 0; i < ll.getChildCount(); i++) {
styleMediaController(ll.getChildAt(i));
}
} else if (view instanceof SeekBar) {
((SeekBar) view).setProgressDrawable(getResources().getDrawable(R.drawable.progressbar));
((SeekBar) view).setThumb(getResources().getDrawable(R.drawable.progresshandle));
}
}
然后,只需调用
styleMediaController(myMC);
【讨论】:
index是什么原因? v.getChildCount() 每次都返回 0。
index从未使用过。
VideoView 的OnPreparedListener 中调用styleMediaController(myMC) 才能使其工作。否则 MediaController 视图没有子视图。
makeControllerView 方法旨在被覆盖,以便您可以提供自己的视图。不幸的是,它现在被隐藏了。
您可能想要获取 MediaController 的源代码并重新实现它或将隐藏的方法复制并粘贴到子类中,以便您可以自定义它。
【讨论】:
makeControllerView 是 protected 对于我正在查看的来源。有人知道它是什么时候从private 变成protected 的吗?
android.widget.MediaController 中的 makeControllerView 的 javadoc 中的 @hide。虽然这个 javadoc 位似乎阻止了 @Override 注释的工作,但它确实 not 实际上阻止了覆盖该方法。最初的问题几乎没有改变 - 这曾经是私人的吗?
我更改了 bk138 答案的代码只更改了元素的颜色。不是可绘制对象本身。此解决方案与支持库 v4 一起兼容旧设备。
private void styleMediaController(View view) {
if (view instanceof MediaController) {
MediaController v = (MediaController) view;
for (int i = 0; i < v.getChildCount(); i++) {
styleMediaController(v.getChildAt(i));
}
} else if (view instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) view;
for (int i = 0; i < ll.getChildCount(); i++) {
styleMediaController(ll.getChildAt(i));
}
} else if (view instanceof SeekBar) {
((SeekBar) view)
.getProgressDrawable()
.mutate()
.setColorFilter(
getResources().getColor(
R.color.MediaPlayerMeterColor),
PorterDuff.Mode.SRC_IN);
Drawable thumb = ((SeekBar) view).getThumb().mutate();
if (thumb instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
//compat mode, requires support library v4
((android.support.v4.graphics.drawable.DrawableWrapper) thumb).setCompatTint(getResources()
.getColor(R.color.MediaPlayerThumbColor));
} else {
//lollipop devices
thumb.setColorFilter(
getResources().getColor(R.color.MediaPlayerThumbColor),
PorterDuff.Mode.SRC_IN);
}
}
}
然后,只需调用
styleMediaController(myMC);
必须在VideoView 的OnPreparedListener 中调用styleMediaController(myMC) 才能使其工作。否则 MediaController 视图没有子视图。
【讨论】:
setCompatTint?