来自外部文件的 setOnClickListener 用于全屏 activity

setOnClickListener from external file for fullscreen activity

我有一个 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;
                }
            }
        });

现在我的问题是如何使用上面的代码为全屏创建一个单独的 class 文件,例如 FullScreenActitvity.java 并添加上面的代码以使用 onclick 从 VideoPlayerActivity.java 开始工作。

据我了解,您需要使用该代码的静态方法:

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;
        }
    }
});