开发者

Change style of android MediaController

Is there a way to customize the MediaController? I need to cha开发者_如何学运维nge the style of buttons, SeekBar etc.


What you can do is recurse the view hierarchy of your MediaController and set the SeekBar's drawable programmatically:

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));
        }
}

Then, just call

styleMediaController(myMC);


The method makeControllerView was meant to be overridden so you could supply your own view. Unfortunately, it is hidden at the moment.

You may want to take the source of MediaController and either reimplement it or copy-and-paste the hidden methods into a subclass so you can customize it.


I changed the code of bk138's answer to just change the color of the elements. Not the drawables themselves. This solution is compatible to old devices in conjunction with the support library 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);
            }
        }
    }

Then, just call

styleMediaController(myMC);

Had to call styleMediaController(myMC) in the OnPreparedListener of the VideoView to make it work. Otherwise the MediaController view has no children.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜