以编程方式更改 MediaController 图标
Posted
技术标签:
【中文标题】以编程方式更改 MediaController 图标【英文标题】:Change MediaController icons programmatically 【发布时间】:2015-04-03 08:38:24 【问题描述】:如何以编程方式更改MediaController
图标?我的意思是播放/暂停/转发和重播。
您看到我添加了 全屏 按钮,但不知道如何更改这三个按钮。
自定义 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;
请帮我更改这些图标的可绘制对象。有可能吗?我真的很困惑那个。
【问题讨论】:
【参考方案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));
【讨论】:
【参考方案2】:如果我理解正确,您使用的是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;
如您所见,它使用私有方法来替换和扩展其视图。
【讨论】:
@AnZ 我不这么认为。这是一个即用型小部件,因此无法像那样修改它,但您可以查看它是如何完成的并实现您完全自己的 MediaController 如果我替换 SDK 中的图标?以上是关于以编程方式更改 MediaController 图标的主要内容,如果未能解决你的问题,请参考以下文章
BottomSheetDialog 内 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面