gpt4 book ai didi

android - 以编程方式更改 MediaController 图标

转载 作者:行者123 更新时间:2023-11-29 20:49:50 25 4
gpt4 key购买 nike

如何以编程方式更改 MediaController 图标?我的意思是播放/暂停/前进和倒退

enter image description here

你看我已经添加了全屏按钮,但不知道如何更改这三个按钮。

自定义 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;
}

请帮我更改这些图标的可绘制对象。有可能吗?我真的很困惑那个。

最佳答案

我最近遇到了同样的问题,并且找到了一个棘手的方法来改变它。首先将 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));
}
}
}
}
}
}
}
}

关于android - 以编程方式更改 MediaController 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29428914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com