Eae
Eae

Reputation: 4321

Setting the size of a SurfaceView with SurfaceHolder.setFixedSize(width,height)

As part of my MainActivity classes layout I have a SurfaceView:

                <LinearLayout
                android:id="@+id/screen_game"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#0000FF"
                android:orientation="vertical"

                >
                           <Button
                        android:id="@+id/button15"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button" />

                           <com.ecslayout.BackgammonBoardView
                               android:id="@+id/backgammonBoardView1"
                               android:layout_width="100dp"
                               android:layout_height="100dp" />



                </LinearLayout>

I have defined for my SurfaceView a skeleton class (I have omitted much of the code for simplicity). Inside surfaceCreated I am trying many different ways to set the size of the canvas. The canvas does not appear to be changing size at all from 100dp x 100dp. All my attempts to call setFixedSize seem to have no effect on size at all:

            public class BackgammonBoardView extends SurfaceView implements SurfaceHolder.Callback {


                public BackgammonBoardView(Context context, AttributeSet attrs) {
                    super(context, attrs);

                    Log.d(TAG,"SurfaceView Constructor.");

                    //register out interest in hearing about changes to our surface
                    SurfaceHolder holder = getHolder();
                    holder.addCallback(this);

                }


                //The method is firing just fine. I have the surfaceCreated output from Log.d
                @Override
                public void surfaceCreated(SurfaceHolder arg0) {

                    //Has no effect on size
                    //this.getHolder().setFixedSize(500, 500);

                    //Has no effect on size
                    //arg0.setFixedSize(500, 500);

                    //Has no effect on size
                    //SurfaceHolder holder = getHolder();
                    //holder.setFixedSize(500, 500);

                    //Has no effect on size
                    /*getHolder().setFixedSize(500,
                            500);*/

                    //Causes Eclipse to enter Debug mode. It crashes the program
                    //android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(500, 500);
                    //setLayoutParams(params);

                    thread.start();
                    Log.d(TAG,"SurfaceView. surfaceCreated.");
                }


            }

One of my questions about setFixedSize is what units do the parameters of the function represent? Are the int's for dot points or perhaps pixels? I'm reading the documentation:

http://developer.android.com/reference/android/view/SurfaceHolder.html

however it just says the parameters for setFixedSize are integers. I don't see yet where it describes what units the integers might represent. Am I perhaps trying to set the size of the SurfaceView from a bad location or is there some other problem? I'm developing targeting API 19 with a min API of 9.

P.S. I think I see what is going on now setFixedSize() changes the resolution of the canvas but not its width and height in the LinearLayout as I was intending to do. I have gotten the screen width and height in the code behind and what I was intending to do is change the width and height of the SurfaceView as it exists in the layout.

P.P.S. I was planning on using the SurfaceView inside of a ScrollView however I'm reading now that approach might not be possible. I'm finding out more from this post:

https://groups.google.com/forum/#!msg/android-developers/zkdnM0G225I/tt_FJGyOkB4J

Thank You

Upvotes: 2

Views: 15993

Answers (1)

Jerry
Jerry

Reputation: 291

You can override the onSizeChanged() function of the SurfaceView class to get & set the size of the view.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

When ever the screen size has changed, this class is called.

Also I noticed your xml code has:

android:layout_width="100dp"
android:layout_height="100dp"

This may be why you can't change the size.

As a second alternative, you may be able to set the size by setting the layout parameters (unsure if this works with ScrollView):

@Override
public void surfaceCreated(SurfaceHolder holder) {
    android.view.ViewGroup.LayoutParams lp = this.getLayoutParams();
    lp.width = 1000; // required width
    lp.height = 1000; // required height
    this.setLayoutParams(lp);
}

Upvotes: 6

Related Questions