最近一直在研究mediaController的位置问题,因为视频只占view的一小部分,想要把mediaController调整到视频上而不是在视频的外部,一直自己在那里尝试设置更重布局和对齐效果,但是没有结果,总是达不到想要的效果。
研究了vidoview的源码,思路清晰了很多,发现videoView的setmediaController的方法调用了attacheMediaController的方法对mediacontroller进行了控制条的添加,再调用setAnchorView的方法,而且控制条现实的时候有一个show函数,代码如下
public void setMediaController(MediaController controller) {
if (mMediaController != null) {
mMediaController.hide();
}
mMediaController = controller;
attachMediaController();
}
private void attachMediaController() {
if (mMediaPlayer != null && mMediaController != null) {
mMediaController.setMediaPlayer(this);
View anchorView = this.getParent() instanceof View ?
(View)this.getParent() : this;
mMediaController.setAnchorView(anchorView);
mMediaController.setEnabled(isInPlaybackState());
}
}
public void setAnchorView(View view) {
mAnchor = view;
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
removeAllViews();
View v = makeControllerView();
addView(v, frameParams);
}
public void show(int timeout) {
if (!mShowing && mAnchor != null) {
setProgress();
if (mPauseButton != null) {
mPauseButton.requestFocus();
}
disableUnsupportedButtons();
int [] anchorpos = new int[2];
mAnchor.getLocationOnScreen(anchorpos);
WindowManager.LayoutParams p = new WindowManager.LayoutParams();
p.gravity = Gravity.TOP;
p.width = mAnchor.getWidth();
p.height = LayoutParams.WRAP_CONTENT;
p.x = 0;
p.y = anchorpos[1] + mAnchor.getHeight() - p.height;
p.format = PixelFormat.TRANSLUCENT;
p.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
p.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
p.token = null;
p.windowAnimations = 0; // android.R.style.DropDownAnimationDown;
mWindowManager.addView(mDecor, p);
mShowing = true;
}
updatePausePlay();
// cause the progress bar to be updated even if mShowing
// was already true. This happens, for example, if we're
// paused with the progress bar showing the user hits play.
mHandler.sendEmptyMessage(SHOW_PROGRESS);
Message msg = mHandler.obtainMessage(FADE_OUT);
if (timeout != 0) {
mHandler.removeMessages(FADE_OUT);
mHandler.sendMessageDelayed(msg, timeout);
}
}
尽管思路清晰了,还是不知道该怎么改。自己闭门研究了好几天不见成效,今天专门在网上找该问题的解决方案,首先注意到一个方案,自定义一个控件实现该功能,原文是这样的:
What i have learning some months ago about Android is this :
When difficultys come , make your own components .
What i mean , that is easy to make your components layout , width 4 or five ImageButtons/Buttons , and link them to the VideoView component functions (or SurfaceView for a better control ).
他说的很好,看到这个让我眼前一亮;但是感觉这个方案稍微有点麻烦,想着应该会有更好的解决方案。于是我就再找发现mediaController.setPadding(0,0,0,100);//设置mediaPlayer的位置left top right bottom。其实这个方法原来就有注意,只是第一次没有调整好,后来也就没有试,这次尝试终于成功了,写出来分享给大家,希望对你有所帮助。