Android适配屏幕,只要一个dimnes

android分辨率很多,android为了适配各种机型,需要写多个dimnes,这种适配方法可以参照,https://blog.csdn.net/qq_30711091/article/details/81318184

但是这样写起来很麻烦

而且今天UI给我的切图是ip6 750*1334,what,怎么办,还要自己换算。。。。。。

好在网络大神告知了一种方法

原理差不多,代码中拿到dpi,再以画图时像素的dpi,计算差值

下面是示意图

 

在手机上展示的效果1080*1920

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个实现视频裁剪并适配屏幕大小的示例方法: ```java public static void cropAndFitToScreen(String videoPath, TextureView textureView) { MediaPlayer mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(videoPath); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); return; } int videoWidth = mediaPlayer.getVideoWidth(); int videoHeight = mediaPlayer.getVideoHeight(); float videoAspectRatio = (float) videoWidth / (float) videoHeight; int viewWidth = textureView.getWidth(); int viewHeight = textureView.getHeight(); float viewAspectRatio = (float) viewWidth / (float) viewHeight; // Calculate the crop width and height to maintain video aspect ratio int cropWidth, cropHeight; if (videoAspectRatio > viewAspectRatio) { cropWidth = (int) (videoHeight * viewAspectRatio); cropHeight = videoHeight; } else { cropWidth = videoWidth; cropHeight = (int) (videoWidth / viewAspectRatio); } // Calculate the crop position int cropX = (videoWidth - cropWidth) / 2; int cropY = (videoHeight - cropHeight) / 2; // Set up the texture view textureView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); SurfaceTexture surfaceTexture = textureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(videoWidth, videoHeight); Surface surface = new Surface(surfaceTexture); // Set up the media player with the cropped video surface mediaPlayer.setSurface(surface); mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); mediaPlayer.setVideoCrop(cropX, cropY, cropWidth, cropHeight); // Start the video playback mediaPlayer.start(); } ``` 这个方法首先创建了一个MediaPlayer对象,并设置其数据源为视频文件路径。然后,它获取视频的宽度和高度,并计算出视频的宽高比。接下来,它获取TextureView的宽度和高度,并计算出TextureView的宽高比。然后,它根据视频和TextureView的宽高比,计算出裁剪后的视频宽度、高度和位置。最后,它设置TextureView的布局参数,创建一个SurfaceTexture对象,并将其作为渲染目标传递给MediaPlayer。 这个示例方法可以让你裁剪视频并适配到TextureView上,但你需要在调用这个方法之前确保TextureView已经被正确地添加到布局中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值