簡體   English   中英

以編程方式更改MediaController圖標

[英]Change MediaController icons programmatically

如何以編程方式更改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;
}

請幫助我更改這些圖標的可繪制對象。 有可能嗎? 我真的很困惑。

如果我對您的理解正確,那么您正在使用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;
    }

如您所見,它使用私有方法來縫合和膨脹其視圖。

我最近遇到了同樣的問題,並且有一個棘手的方法來更改它。 首先,將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));
                        }
                    }
                }
            }
        }
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM