我有一个VideoPlayerActivity在exoPlayer中播放视频,我在其中加入了 FullScreen
选项与 setOnClickListener
我能够使视频播放器在全屏播放。
这里是代码。
boolean fullscreen = false;
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fullscreen) {
imageView1.setImageDrawable(ContextCompat.getDrawable(VideoPlayerActivity.this, R.drawable.ic_fullscreen_open));
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
if(getSupportActionBar() != null){
getSupportActionBar().show();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
fullscreen = false;
}else{
imageView1.setImageDrawable(ContextCompat.getDrawable(VideoPlayerActivity.this, R.drawable.ic_fullscreen_close));
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
if(getSupportActionBar() != null){
getSupportActionBar().hide();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
fullscreen = true;
}
}
});
现在我的问题是我如何用上面的代码为全屏制作一个单独的类文件,就像 FullScreenActitvity.java
并添加了上述代码,以便从 VideoPlayerActivity.java
与onclick.I有一个VideoPlayerActivity在exoPlayer中播放视频,我在setOnClickListener中添加了FullScreen选项,我能够使视频播放器全屏播放。
根据我的理解,你需要静态方法与该代码。
imageView1.setImageDrawable(ContextCompat.getDrawable(VideoPlayerActivity.this, R.drawable.ic_fullscreen_open));
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
if(getSupportActionBar() != null){
getSupportActionBar().show();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
fullscreen = false;
所以,这是你的最终代码:
FullScreenActitvity. java
static boolean method(ImageView imageView1, View playerView, Context context) {
imageView1.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_fullscreen_open));
((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
if(getSupportActionBar() != null){
((AppCompatActivity) context).getSupportActionBar().show();
}
((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
return false;
}
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fullscreen) {
fullscreen = FullScreenActitvity.method(imageView1, playerView, VideoPlayerActivity.this);
}else{
imageView1.setImageDrawable(ContextCompat.getDrawable(VideoPlayerActivity.this, R.drawable.ic_fullscreen_close));
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
if(getSupportActionBar() != null){
getSupportActionBar().hide();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
fullscreen = true;
}
}
});