以编程方式更改MediaController图标

时间:2015-04-03 08:38:25

标签: android android-layout android-videoview mediacontroller

如何以编程方式更改MediaController图标?我的意思是播放/暂停/转发和重新

enter image description here

您看到我添加了全屏按钮,但无法实现如何更改这三个按钮。

Custom MediaController类:

public class CustomVideoController extends MediaController {
public static boolean full = false;

private ImageButton fullScreenBtn;
Context mContext;
Activity activity;
VideoView mVideoView;

public CustomVideoController(Context context, Activity activity, VideoView videoView) {
    super(new ContextThemeWrapper(context, R.style.VideoPlayerTheme));
    this.activity = activity;
    mContext = context;
    mVideoView = videoView;
}

@Override
public void setAnchorView(View view) {
    super.setAnchorView(view);
    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    frameParams.gravity = Gravity.RIGHT | Gravity.TOP;
    frameParams.setMargins(12,14,10,12);

    View v = AddFullScreenBtn();
    addView(v, frameParams);
}

@Override
public void hide() {
    super.hide();
}

@Override
public void show(int timeout) {
    super.show(0);
}

private View AddFullScreenBtn() {
    fullScreenBtn = new ImageButton(mContext);
    if (full)
        fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_stretch);
    else
        fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_shrink);
    fullScreenBtn.setBackgroundColor(Color.WHITE);
    fullScreenBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (full){
                fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_stretch);
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                full = false;
            } else {
                fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_shrink);
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                full = true;
            }
        }
    });
    return fullScreenBtn;
}

请帮我改变那些图标的drawable。有可能吗?我真的很困惑那一个。

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您就会使用android.widget.MediaControleller。在这种情况下,无法更改布局按钮。这就是原因(代码吼叫来自 MediaController.class ):

/**
     * Create the view that holds the widgets that control playback.
     * Derived classes can override this to create their own.
     * @return The controller view.
     * @hide This doesn't work as advertised
     */
    protected View makeControllerView() {
        LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRoot = inflate.inflate(com.android.internal.R.layout.media_controller, null);

        initControllerView(mRoot);

        return mRoot;
    }

正如您所看到的,它使用私有方法进行搜索和扩充其视图。

答案 1 :(得分:1)

我最近也遇到了同样的问题,并且找到了一种棘手的方法来进行更改。首先,将MediaController类扩展为客户类,然后按如下所示重写setAnchorView()。不太确定它是否足够安全。

public class MyMediaController extends MediaController {
public MyMediaController(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyMediaController(Context context, boolean useFastForward) {
    super(context, useFastForward);
}

public MyMediaController(Context context) {
    super(context);
}

@Override
public void setAnchorView(View view) {
    super.setAnchorView(view);
    //find the control buttons
    for(int index = 0; index< getChildCount(); ++index) {
        View nextChild = getChildAt(index);
        if (nextChild instanceof LinearLayout)
        {
            LinearLayout linearLayout = (LinearLayout)nextChild;
            for (int i = 0; i < linearLayout.getChildCount(); i++)
            {
                if (i == 0 &&  linearLayout.getChildAt(i) instanceof LinearLayout)
                {
                    LinearLayout linearLayoutControlPanel = (LinearLayout)linearLayout.getChildAt(i);
                    int len = linearLayoutControlPanel.getChildCount();
                    if (len > 0) {
                        // index = 0, pre button, just hide it
                        View buttonPre = linearLayoutControlPanel.getChildAt(0);
                        if (buttonPre instanceof ImageButton) {
                            buttonPre.setVisibility(INVISIBLE);
                        }
                        // index = len - 1, next button, change icon to fullscreen
                        View buttonNex = linearLayoutControlPanel.getChildAt(len - 1);
                        if (buttonNex instanceof ImageButton) {
                            ((ImageButton) buttonNex).setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_fullscreen));
                        }
                    }
                }
            }
        }
    }
}

}