• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How could I resize the camera preview dynamically?

I want to resize the camera preview dynamically but always fail.

I start my preview by setting the size as follow
<SurfaceView
android:id="@+id/mSurfaceView1"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="visible"
android:layout_width="480px"
android:layout_height="320px">
</SurfaceView>

and add a button to resize it to 320 * 240.
Code:
resizeButton.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(320, 240);
                surfaceView.setLayoutParams(params);                
            }
        });
But this can't work.What steps do I miss?
Should I just start a new activity with different size of the surfaceView?
Thanks a lot
 
I solved it by myself after a lot of trials and errors

Code:
               resetCamera();  //reset the camera                        
                
                ViewGroup.LayoutParams params = surfaceView.getLayoutParams();                     
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);               
                params.width =dm.widthPixels;
                params.height = dm.heightPixels;
                surfaceView.setLayoutParams(params);
                                               
                initCamera(); //initiate the camera(open camera, set parameter, setPreviewDisplay, startPreview)
 
  • Like
Reactions: Yerevak
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones