There is a VideoView in which the video is played. The video does not fit in height, the height becomes smaller and the video is too thick. How to make the thickness adjust to the height?
2 answers
Suppose you have a markup:
... <FrameLayout android:id="@+id/video_view" android:layout_width="200dp" android:layout_height="match_parent"> <VideoView android:id="@+id/video_player_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" /> <ProgressBar android:id="@+id/video_player_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" /> </FrameLayout> ... Then in the activation it is implemented like this:
public class VideoActivity extends AppCompatActivity { FrameLayout videoContainer; VideoView videoView; MediaController mediaController; @Override protected void onCreate(Bundle savedInstanceState) { ... mediaController = new MediaController(this); videoView.setOnPreparedListener(mp -> { mp.setOnVideoSizeChangedListener((mp1, width, height) -> { adjustAspectRatio(width, height); // Re-Set the videoView that acts as the anchor for the MediaController mediaController.setAnchorView(videoView); }); }); } private void adjustAspectRatio(int videoWidth, int videoHeight) { int viewHeight = videoContainer.getHeight(); double videoAspectRatio = (double) videoWidth / videoHeight; ViewGroup.LayoutParams lp = videoContainer.getLayoutParams(); // limited by narrow width; restrict height lp.width = (int) (viewHeight * videoAspectRatio); videoContainer.setLayoutParams(lp); } } |
VideoView must necessarily lie inside FrameLayout, then the proportions are preserved.
|