VideoView 万能适配
来源:互联网 发布:windows dhcp client 编辑:程序博客网 时间:2024/05/22 04:26
用VideoView进行视频播放的时候,因为视频资源本身尺寸原因,如果VideoView不做任何处理,播放效果总不尽人意。
对VideoView进行了简单封装,但是已经达到了想要的播放效果。
我想要的效果是:
代码:
public class CustomVideoView extends VideoView { //最终的视频资源宽度 private int mVideoWidth=480; //最终视频资源高度 private int mVideoHeight=480; //视频资源原始宽度 private int videoRealW=1; //视频资源原始高度 private int videoRealH=1; public CustomVideoView(Context context) { super(context); // TODO Auto-generated constructor stub } public CustomVideoView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public CustomVideoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override public void setVideoPath(String path) { super.setVideoPath(path); MediaMetadataRetriever retr = new MediaMetadataRetriever(); retr.setDataSource(path); String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); // 视频高度 String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // 视频宽度 try { videoRealH=Integer.parseInt(height); videoRealW=Integer.parseInt(width); } catch (NumberFormatException e) { Log.e("----->" + "VideoView", "setVideoPath:" + e.toString()); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); if(height>width){ //竖屏 if(videoRealH>videoRealW){ //如果视频资源是竖屏 //占满屏幕 mVideoHeight=height; mVideoWidth=width; }else { //如果视频资源是横屏 //宽度占满,高度保存比例 mVideoWidth=width; float r=videoRealH/(float)videoRealW; mVideoHeight= (int) (mVideoWidth*r); } }else { //横屏 if(videoRealH>videoRealW){ //如果视频资源是竖屏 //宽度占满,高度保存比例 mVideoHeight=height; float r=videoRealW/(float)videoRealH; mVideoWidth= (int) (mVideoHeight*r); }else { //如果视频资源是横屏 //占满屏幕 mVideoHeight=height; mVideoWidth=width; } } if(videoRealH==videoRealW&&videoRealH==1){ //没能获取到视频真实的宽高,自适应就可以了,什么也不用做 super.onMeasure(widthMeasureSpec,heightMeasureSpec); }else { setMeasuredDimension(mVideoWidth, mVideoHeight); } } @Override public boolean onTouchEvent(MotionEvent ev) { //屏蔽触摸点击事件 return true; }}
改变屏幕方向,它会自动适应
1 0
- VideoView 万能适配
- 屏幕万能适配
- VideoView
- videoView
- VideoView
- VideoView
- VideoView
- android开发之万能屏幕适配
- 万能适配,百分比布局,支持padding
- Android autolayout万能的屏幕适配
- 像素万能适配转换的工具类
- android之实现万能适配RecyclerView的adapter
- Android屏幕适配万能尺寸生成方法
- 解决vitamio的mediaController总是在底部无法适配videoview的问题
- Android-- VideoView
- VideoView笔记
- MediaController+VideoView
- 11 VideoView
- Python 正则表达式的使用(re findall finditer match search)- 千月的python linux 系统管理指南学习笔记(13)
- oracle x$bh 说明
- 快速排序总结及poj代码
- [JS]IE兼容模式下会把后台传来的html或者xml标签变成大写的解决方法
- easyX实现鼠标人物移动(可视化编程)
- VideoView 万能适配
- 远程服务调用之RMI、Hessian、Burlap、Spring的HTTPinvoker
- @RequestBody, @ResponseBody 注解详解
- 设计模式分类
- 学会nodejs-成为全栈工程师--持续更新
- trace32常用命令备忘
- JS那些事儿(2)-数据类型和变量
- Myeclipse2016破解
- 回到顶部效果