我正在实施自己的VideoView
版本。该课程包含一个播放暂停按钮ImageView
,覆盖VideoView
。一切都被FrameLayout
包裹着。要使用此类,我调用一个自定义初始化方法,该方法从其父级中删除该类,以编程方式创建FrameLayout
和ImageView
,然后将VideoView
和ImageView
添加到FrameLayout
。最后,FrameLayout
作为原始索引添加到VideoView
的父级。问题是该视图对用户不可见,但我可以看到父进程在调试时将FrameLayout作为其子进程。
public class PodoVideoView extends VideoView {
// boilerplate constructors removed
// This method must be called to set the functionality of this class.
// reasoning: cannot call getParent() from constructor
public void init(Activity activity) {
// remove VideoView and save its index
ViewGroup parent = (ViewGroup) this.getParent();
int indexInParent = parent.indexOfChild(this);
parent.removeView(this);
// setup layout
final FrameLayout videoViewLayout = new FrameLayout(activity);
videoViewLayout.setBackgroundColor(activity.getResources().getColor(R.color.black));
videoViewLayout.setLayoutParams((new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)));
// add VideoView and play/pause ImageView to layout
videoViewLayout.addView(this);
final ImageView playPauseImageView = new ImageView(activity);
playPauseImageView.setImageResource(R.drawable.video_play);
videoViewLayout.addView(playPauseImageView);
// add layout to parent
parent.addView(videoViewLayout, indexInParent);
// play/pause UX
setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(isPlaying()) {
playPauseImageView.setVisibility(View.VISIBLE);
mp.pause(); // if this doesn't work, use mp.pause() instead of pause()
} else {
playPauseImageView.setVisibility(View.INVISIBLE);
mp.start();
}
}
});
}
});
setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playPauseImageView.setVisibility(View.VISIBLE);
}
});
}
答案 0 :(得分:2)
尝试playPauseImageView.bringToFront();
并且您应该在将视图添加到viewGroup
LayoutParams
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
videoViewLayout.addView(playPauseImageView, layoutParams);
playPauseImageView.bringToFront();