如何使用android.view.SurfaceView

时间:2013-09-02 11:19:44

标签: android android-canvas surfaceview

我正在尝试使用SurfaceView小部件。我有一个班级

public class OurView extends SurfaceView implements Runnable {
    public OurView(Context context) {
        super(context);
        Log.d("OurView", "Yay We started");
    }

    @Override
    public void run() {

    }
}

所以我在Activity的XML中有一个SurfaceView小部件。然后我尝试在onCreate方法

中绑定它
TheView = (OurView) findViewById(R.id.theSurfaceView);

问题出在这里我收到错误,我无法将OurView强制转换为android.view.SurfaceView

我知道我通常可以在onCreateMethod

中对视图本身执行此操作
TheView = new OurView(this);
setContentView(TheView);

事情是我不想这样做,我不想在我的整个活动上画画,我只想在我放在布局上的SurfaceView小部件上画画。我该怎么做?

1 个答案:

答案 0 :(得分:3)

您应该将自定义的SurfaceView com.xx.OurView放在布局中,而不是SurfaceView