Resizing TextureView at Runtime

I am working on streaming mp4 to textView for the application I am currently working on. I have a TextureView displaying a video, however I need to resize it to fit the screen when rotated. After much trial and error, it seems that the problem is that the TextureView cannot be larger than the containing view. I also tried to resize the container view but after that I was unable to center the TextureView correctly on the screen.

 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>

      

+3


source to share


1 answer


Try this link:

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));
}

      

+2


source







All Articles