首页 > 移动平台 > 详细

android SurfaceView中播放视频 按视频的原始比例播放

时间:2015-10-29 16:08:23      阅读:2667      评论:0      收藏:0      [点我收藏+]
    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();
        }
    };

 

android SurfaceView中播放视频 按视频的原始比例播放

原文:http://www.cnblogs.com/bloodofhero/p/4920659.html

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!