zoukankan      html  css  js  c++  java
  • android SurfaceView中播放视频 按视频的原始比例播放

        OnPreparedListener mediaPlayerOnPreparedListener = new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer arg0) {
                // 首先取得video的宽和高
                int vWidth = mediaPlayer.getVideoWidth();
                int vHeight = mediaPlayer.getVideoHeight();
                
                // 该LinearLayout的父容器 android:orientation="vertical" 必须
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutPlay);
                int lw = linearLayout.getWidth();
                int lh = linearLayout.getHeight();
    
                if (vWidth > lw || vHeight > lh) {
                    // 如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
                    float wRatio = (float) vWidth / (float) lw;
                    float hRatio = (float) vHeight / (float) lh;
    
                    // 选择大的一个进行缩放
                    float ratio = Math.max(wRatio, hRatio);
                    vWidth = (int) Math.ceil((float) vWidth / ratio);
                    vHeight = (int) Math.ceil((float) vHeight / ratio);
    
                    // 设置surfaceView的布局参数
                    LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(vWidth, vHeight);
                    lp.gravity = Gravity.CENTER;
                    surfaceView.setLayoutParams(lp);
                }
                // 然后开始播放视频
                mediaPlayer.start();
            }
        };
  • 相关阅读:
    C语言位操作
    Ribbon负载规则的替换
    Nginx 的配置文件
    Nginx 操作常用的命令
    Nginx 是什么?
    SpringCloud Eureka 新版本依赖
    @Autowired 与@Resource的区别
    spring 注释
    redis 的 rdb 和 aof 持久化的区别
    jdk1.7下HashMap的头插法问题
  • 原文地址:https://www.cnblogs.com/bloodofhero/p/4920659.html
Copyright © 2011-2022 走看看