반응형
WebChromeClient
webChromeClient의 기본 설정에는 html의 비디오 태그를 전체화면 할 수 있는 기능이 없어서 WebChromeClient 클래스를 상속받은 새로운 클래스를 생성해야 한다.
onShowCustomView, onHideCustomView 메서드를 오버라이드해 onShowCustomView에서는 전체화면이 되는 기능을 구현하고 onHideCustomView 에는 전체화면 종료시 화면이 바뀌도록 기능을 구현하면 된다.
코드
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
// WebChromeClient 설정
webView.setWebChromeClient(new FullscreenWebChromeClient(this));
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
}
/**
* 비디오 태그 전체화면 설정 클래스
*/
public class FullscreenWebChromeClient extends WebChromeClient {
private View customView;
private FrameLayout fullscreenContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private Activity activity;
public FullscreenWebChromeClient(Activity activity) {
this.activity = activity;
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (customView != null) {
callback.onCustomViewHidden();
return;
}
customView = view;
customViewCallback = callback;
fullscreenContainer = new FrameLayout(view.getContext());
fullscreenContainer.addView(customView, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
getWindow().addContentView(fullscreenContainer, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
fullscreenContainer.setVisibility(View.VISIBLE);
// 가로 모드로 전환
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
@Override
public void onHideCustomView() {
if (customView == null) {
return;
}
fullscreenContainer.setVisibility(View.GONE);
((FrameLayout) getWindow().getDecorView()).removeView(fullscreenContainer);
customView = null;
fullscreenContainer = null;
customViewCallback.onCustomViewHidden();
// 기본 방향으로 전환
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
);
}
}
반응형
'Android' 카테고리의 다른 글
Android Splash 화면 만들기 (0) | 2023.04.26 |
---|---|
전체 화면 설정 (0) | 2023.04.21 |
RecyclerView 만들기 (0) | 2023.04.18 |
RecyclerView (0) | 2023.04.16 |
VideoView로 동영상 재생하기 (0) | 2023.04.12 |