IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android VideoView的布局适配 -> 正文阅读

[移动开发]Android VideoView的布局适配

最近有一个需求:在删除视频文件前预览一下。预览区域不大。但是能满足所有宽高比例的视频。这就要针对每一个视频文件做适配。

VideoView和ImageView不一样,ImageView有ScaleType来处理这个问题,但是VideoView不行。

完成这个功能需要以下个步骤:

1. 获取视频的宽高。

        try {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(path);
            String widthString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
            String heightString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);

            videoWidth = Integer.parseInt(widthString);
            videoHeight = Integer.parseInt(heightString);
        
        } catch (Exception e) {
            e.printStackTrace();
        }

2. 重写VideoView的onMeasured(), 采用默认的方式。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(getWidth(), widthMeasureSpec);
        int height = getDefaultSize(getHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

3. 根据视频宽高比,裁剪VideoView的宽高。有四种情况需要处理。

  • 视频宽高都比显示区域小。直接取视频宽高。
  • 视频只有宽都比显示区域宽大。视频宽取显示区域的最大宽, 并以此为基准,算出视频高。
  • 视频只有高都比显示区域高大。视频高取显示区域的最大高, 并以此为基准,算出视频宽。
  • 视频宽高都比显示区域大。递归处理,以上两种情况。
    private Rect getMeasuredRect(int videoWidth, int videoHeight, int contentWidth, int contentHeight) {
        float measuredWidth = 0;
        float measuredHeight = 0;

        if (videoWidth <= contentWidth && videoHeight <= contentHeight) {
            measuredWidth = videoWidth;
            measuredHeight = videoHeight;
        }

        if (videoWidth > contentWidth && videoHeight <= contentHeight) {
            measuredWidth = contentWidth;
            measuredHeight = measuredWidth * ((float) videoHeight / (float) videoWidth);
        }

        if (videoHeight > contentHeight && videoWidth <= contentWidth) {
            measuredHeight = contentHeight;
            measuredWidth = measuredHeight * ((float) videoWidth / (float) videoHeight);
        }

        if (videoHeight > contentHeight && videoWidth > contentWidth) {
            measuredWidth = contentWidth;
            measuredHeight = (float) contentWidth * (float) videoHeight / (float) videoWidth;
            return getMeasuredRect((int) measuredWidth, (int) measuredHeight, contentWidth, contentHeight);
        }

        Rect rect = new Rect();
        rect.right = (int) measuredWidth;
        rect.bottom = (int) measuredHeight;

        return rect;
    }

完成以上三步骤后,视频就能完美适配布局了。

完整代码如下:

public class MatchParentVideoView extends VideoView {
    private int videoWidth = 0;
    private int videoHeight = 0;
    private int videoRotation = 0;
    private int contentWidth = 0;
    private int contentHeight = 0;

    public int getVideoWidth() {
        return videoWidth;
    }

    public int getVideoHeight() {
        return videoHeight;
    }


    public MatchParentVideoView(Context context) {
        this(context, null);
    }

    public MatchParentVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MatchParentVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(getWidth(), widthMeasureSpec);
        int height = getDefaultSize(getHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    public void setVideoPath(String path) {
        try {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(path);
            String widthString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
            String heightString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
            String rotationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);

            videoWidth = Integer.parseInt(widthString);
            videoHeight = Integer.parseInt(heightString);
            int tempRotation = Integer.parseInt(rotationString);
            videoRotation = (tempRotation % 360 + 360) % 360;
            if (videoRotation == 90 || videoRotation == 270) {
                int temp = videoWidth;
                videoWidth = videoHeight;
                videoHeight = temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.setVideoPath(path);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (contentHeight > 0 && contentWidth > 0) { // always call onSizeChanged, make sure only do measure rect once.
            return;
        } else {
            contentHeight = h;
            contentWidth = w;

            int width = getVideoWidth();
            int height = getVideoHeight();
            if (width > 0 && height > 0) { //make sure validate video
                float measuredWidth = 0;
                float measuredHeight = 0;

                Rect measureRect = getMeasuredRect(width, height, contentWidth, contentHeight);
                measuredWidth = measureRect.width();
                measuredHeight = measureRect.height();

                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.height = (int) measuredHeight;
                layoutParams.width = (int) measuredWidth;
                layoutParams.leftMargin = Math.max(0, (contentWidth - (int) measuredWidth) / 2);
                layoutParams.topMargin = Math.max(0, (contentHeight - (int) measuredHeight) / 2);
                setLayoutParams(layoutParams);
                requestLayout();
            }
        }
    }

    private Rect getMeasuredRect(int videoWidth, int videoHeight, int contentWidth, int contentHeight) {
        float measuredWidth = 0;
        float measuredHeight = 0;

        if (videoWidth <= contentWidth && videoHeight <= contentHeight) {
            measuredWidth = videoWidth;
            measuredHeight = videoHeight;
        }

        if (videoWidth > contentWidth && videoHeight <= contentHeight) {
            measuredWidth = contentWidth;
            measuredHeight = measuredWidth * ((float) videoHeight / (float) videoWidth);
        }

        if (videoHeight > contentHeight && videoWidth <= contentWidth) {
            measuredHeight = contentHeight;
            measuredWidth = measuredHeight * ((float) videoWidth / (float) videoHeight);
        }

        if (videoHeight > contentHeight && videoWidth > contentWidth) {
            measuredWidth = contentWidth;
            measuredHeight = (float) contentWidth * (float) videoHeight / (float) videoWidth;
            return getMeasuredRect((int) measuredWidth, (int) measuredHeight, contentWidth, contentHeight);
        }

        Rect rect = new Rect();
        rect.right = (int) measuredWidth;
        rect.bottom = (int) measuredHeight;

        return rect;
    }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 07:56:17  更:2021-07-28 07:58:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 10:45:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码