2014-09-03 3442 views
2

由于我MainActivity类的布局部分我有一个SurfaceView:与SurfaceHolder.setFixedSize(宽度,高度)设置一个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> 

我为我的SurfaceView骨架类中定义的(I省略很多简单的代码)。 Inside surfaceCreated我正在尝试使用许多不同的方式来设置画布的大小 。从100dp x 100dp开始,画布看起来并没有改变大小。我想尽办法叫setFixedSize似乎在所有的大小没有影响:

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


      } 

我的一个约setFixedSize问题是单位做什么函数的参数代表什么?是int点还是像素?我正在阅读文档:

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

但是它只是说setFixedSize的参数是整数。我还没有看到它在哪里描述了整数可能代表的单位。我是否试图设置一个糟糕的位置SurfaceView的大小 还是有一些其他问题?我正在开发针对API 19,API API最小为9.

P.S.我想我现在看到现在发生了什么setFixedSize()会改变画布的分辨率,但不会按照我打算做的那样改变LinearLayout中的宽度和高度。我在后面的代码中获得了屏幕宽度和高度,我打算做的是更改布局中存在的SurfaceView的宽度和高度。

P.P.S.我正计划在ScrollView中使用SurfaceView,但我现在正在阅读的方法可能无法实现。我更从这篇文章找出:

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

谢谢

+0

要查看'setFixedSize()'在行动,请参阅“硬件Scaler锻炼者“的活动Grafika(https://github.com/google/grafika)。快速演示在这里:https://www.youtube.com/watch?v=qpR​​aD-ij2xc – fadden 2014-09-04 15:41:15

回答

5

您可以覆盖SurfaceView类的onSizeChanged()函数来获取&设置视图的大小。

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

当屏幕尺寸改变时,调用这个类。

而且我注意到你的XML代码有:

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

这可能就是为什么你不能改变大小。

作为第二选择,您可以通过设置布局参数(不能确定这是否与滚动型作品)来设定大小:

@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); 
} 
+0

我在@Override上的方法,我看到它被称为正确的构造函数之后,无论我是否调用setFixedSize ()。尽管如此,我仍然无法改变Surfaced的大小,它被卡在100dp x 100dp。我在黑色背景下看到它。 – Giuseppe 2014-09-04 03:22:15

+0

这很了不起。感谢您发布此信息。 – Giuseppe 2014-09-04 07:03:00

+0

不客气:)很高兴我能帮上忙。 – Jerry 2014-09-04 07:05:23