在运行时更改TextureView大小

时间:2014-08-18 08:35:55

标签: java android

我正在努力将mp4流式传输到我正在处理的应用的textureView。我有TextureView显示视频,但我需要调整大小以匹配旋转时的屏幕大小。经过多次试验和错误后,似乎问题是TextureView不能比包含视图大。我还试图调整容器视图的大小,但我无法在屏幕上正确地居中TextureView。

 public void onOrientationChanged(int orientation) {
                if(isLandscape(orientation)){
                    myTexture.setRotation(-90);
                    RelativeLayout.LayoutParams params = new           RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = height;
                    params.height = (height * 9)/16;
                    params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);


                    myTexture.setLayoutParams(params);
                    myTexture.getLayoutParams().height = (height * 9)/16;
                    myTexture.getLayoutParams().width = height;

                    rl.requestLayout();
                    rl.invalidate();
                    rl.recomputeViewAttributes(myTexture);

                    Log.v("View Size", "Width tex: " + myTexture.getWidth());
                    Log.v("View Size", "Height tex: " + myTexture.getHeight());
                    Log.v("View Size", "Width tex parent: " + rl.getWidth());
                    Log.v("View Size", "Height tex parent : " + rl.getHeight());

                }


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity"
    android:id="@+id/mediaParent"
    android:layout_centerInParent="true"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp">

        <TextureView
            android:layout_width="360dp"
            android:layout_height="203dp"
            android:id="@+id/surface"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />



</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

尝试此链接:

http://www.binpress.com/tutorial/video-cropping-with-texture-view/21

private void initView() {
    mTextureView = (TextureView) findViewById(R.id.textureView);
    // SurfaceTexture is available only after the TextureView
    // is attached to a window and onAttachedToWindow() has been invoked.
    // We need to use SurfaceTextureListener to be notified when the SurfaceTexture
    // becomes available.
    mTextureView.setSurfaceTextureListener(this);

    FrameLayout rootView = (FrameLayout) findViewById(R.id.rootView);
    rootView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_UP:
                    updateTextureViewSize((int) motionEvent.getX(), (int) motionEvent.getY());
                    break;
            }
            return true;
        }
    });
}

private void updateTextureViewSize(int viewWidth, int viewHeight) {
    mTextureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
}