public static Size getOptimalDisplaySize(Context context, int videoWidth, int videoHeight) {
int orientation = context.getResources().getConfiguration().orientation;
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
float screenScale = (orientation == Configuration.ORIENTATION_PORTRAIT ? ((float) screenWidth / screenHeight) : ((float) screenHeight / screenWidth));
float videoScale = (orientation == Configuration.ORIENTATION_PORTRAIT ? ((float) videoWidth / videoHeight) : ((float) videoHeight / videoWidth));
int screenMin = (orientation == Configuration.ORIENTATION_PORTRAIT ? screenWidth : screenHeight);
int screenMax = (orientation == Configuration.ORIENTATION_PORTRAIT ? screenHeight : screenWidth);
int surfaceMax = (screenScale < videoScale ? screenMin : screenMax);
int surfaceMin = (int) (screenScale <= videoScale ? ((float) surfaceMax / videoScale) : ((float) surfaceMax * videoScale));
return (videoWidth > videoHeight ? new Size(surfaceMax, surfaceMin) : new Size(surfaceMin, surfaceMax));
}
MediaPlayer + SurfaceView播放视频时,保持视频原始比例的方法。
最新推荐文章于 2021-07-30 22:36:58 发布