Ste*_*hih 2 java android surfaceview
这是一个简单的问题,但我无法弄清楚:
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GG.My_pic.testA
android:id = "@+id/myview"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在月球着陆器中,主线程使用
// tell system to use the layout defined in our XML file
setContentView(R.layout.lunar_layout);
Run Code Online (Sandbox Code Playgroud)
但我不能用我的
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Run Code Online (Sandbox Code Playgroud)
如果我将R.layout.main更改为 - > new testA(this),它就可以了
(testA是扩展SurfaceView实现SurfaceHolder.Callback的类)
为什么??
我发现了原因,但我不知道为什么.
作为java和android的真正初学者,我花了很长时间才发现.
这个问题的关键是
class gameView扩展SurfaceView实现SurfaceHolder.Callback {
public gameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
Run Code Online (Sandbox Code Playgroud)
}
你可以在每个教程中看到,这是surfaceview的基础
如
http://android-er.blogspot.com/2010/05/android-surfaceview.html
http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html
surfaceview中有3种构造函数:
SurfaceView(Context context)
SurfaceView(Context context, AttributeSet attrs)
SurfaceView(Context context, AttributeSet attrs, int defStyle)
Run Code Online (Sandbox Code Playgroud)
我花了一天时间使用第一个:
SurfaceView(Context context)
Run Code Online (Sandbox Code Playgroud)
但是当我转向第二个构造函数时:
SurfaceView(Context context, AttributeSet attrs)
Run Code Online (Sandbox Code Playgroud)
它突然起作用了!
这是解决方案.
谁能告诉我为什么?
| 归档时间: |
|
| 查看次数: |
3662 次 |
| 最近记录: |