我有一个TextureView,可以在其中播放视频。
我的方向锁定为纵向,并且当我横向录制视频时,我也需要横向显示图像(旋转90度和保留长宽比,而方面填充不适合)。
我设法使它在高度大于宽度时起作用,但反之则不行:
private void adjustAspectRatio(int videoWidth, int videoHeight, TextureView mTextureView, int degrees) {
int viewWidth = mTextureView.getWidth();
int viewHeight = mTextureView.getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
int newWidth, newHeight;
if (viewHeight > (int) (viewWidth * aspectRatio)) {
// limited by narrow width; restrict height
newWidth = (int) (viewWidth * aspectRatio);
newHeight = viewHeight;
} else {
// limited by short height; restrict width
newWidth = (int) (viewHeight / aspectRatio);
newHeight = viewHeight;
}
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
Log.v(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(degrees); // rotate the image a number of degrees specified.
txform.postTranslate(xoff, yoff);
mTextureView.setTransform(txform);
}
做到这一点并使之起作用的最佳方法是什么?