Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TextureView Size at Runtime

Tags:

java

android

I am working on streaming a mp4 to a textureView for a app i am currently working on. i have the TextureView displaying the video however i need to resize it to match the screen size on rotation. After much trial and error it seems that the problem is that the TextureView cannot be made larger than the containing view. I have also tried to resize the container view but i am then unable to center the TextureView correctly on 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>
like image 943
kgpmurray Avatar asked Oct 20 '22 03:10

kgpmurray


1 Answers

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));
}
like image 137
vishal arote Avatar answered Oct 23 '22 09:10

vishal arote