在setRotation()之后如何使TextureView适合屏幕

时间:2016-02-24 12:09:14

标签: android video rotation textureview

我有两个TextureView来显示视频。当我同时调用setRotation(90)方法时,它会旋转它们,但每个视图都不适合屏幕。下图显示了它的外观。我只需要使用横向屏幕方向。 enter image description here 我该怎么办呢? 这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/video_layout_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextureView
            android:id="@+id/video_screen_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/video_layout_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextureView
            android:id="@+id/video_screen_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

好的,我使用Grafika方法

解决了这个问题
private void adjustAspectRatio(int videoWidth, int videoHeight) {
    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 = 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.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.postTranslate(xoff, yoff);
    mTextureView.setTransform(txform);
}