コード例 #1
0
        //  *
        //   * Sets the TextureView transform to preserve the aspect ratio of the video.
        //
        private void AdjustAspectRatio(int videoWidth, int videoHeight)
        {
            int    viewWidth   = mTextureView.Width;
            int    viewHeight  = mTextureView.Height;
            double aspectRatio = (double)videoHeight / videoWidth;

            int newWidth, newHeight;

            if (viewHeight > (int)(viewWidth * aspectRatio))
            {
                // limited by narrow width; restrict height
                newWidth  = viewWidth;
                newHeight = (int)(viewWidth * aspectRatio);
            }
            else
            {
                // limited by short height; restrict width
                newWidth  = (int)(viewHeight / aspectRatio);
                newHeight = viewHeight;
            }
            int xoff = (viewWidth - newWidth) / 2;
            int yoff = (viewHeight - newHeight) / 2;

            Log.Verbose(TAG, "video=" + videoWidth + "x" + videoHeight + " view=" + viewWidth + "x" + viewHeight + " newView=" + newWidth + "x" + newHeight + " off=" + xoff + "," + yoff);

            Matrix txform = new Matrix();

            mTextureView.GetTransform(txform);
            txform.SetScale((float)newWidth / viewWidth, (float)newHeight / viewHeight);
            //txform.postRotate(10);          // just for fun
            txform.PostTranslate(xoff, yoff);
            mTextureView.SetTransform(txform);
        }