我对surfaceview创建有疑问。我几乎已经阅读了与此主题相关的大多数讨论,但它们都不适合我的场景。我的apk有点特别。由于某些特殊原因,我需要动态创建Relativelayout。不是通过布局xml文件然后是R.layouout。我已经将我的代码提取到一个简单的应用程序来说明我的问题。
我有2个文件,即MainActivity.java和BrowserView.java。在MainActivity.java中,我将创建一个BrowserView。该类实现了surfaceview。
我已尝试在surfaceCreated和surfaceChanged中设置断点,但到目前为止,这些事件中没有一个表明我的表面创建不成功。我有什么遗漏的吗?
我在surfaceview创作中的概念是否正确?您可以在我的代码中发现任何建议或任何问题?非常感谢!
public class MainActivity extends ActionBarActivity {
RelativeLayout mRelativeLayout = null;
private BrowserView mBrowserView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRelativeLayout = new RelativeLayout(this);
setContentView(mRelativeLayout);
mBrowserView = new BrowserView(this);
// I did this because some discussion suggested that height and
//width should not be 0
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lp.height = 130;
lp.width = 508;
mRelativeLayout.setLayoutParams(lp);
mRelativeLayout.addView(mBrowserView, lp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class BrowserView extends SurfaceView implements SurfaceHolder.Callback {
public BrowserView(Context context) {
super(context);
// TODO Auto-generated constructor stub
// make sure we get key events
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
// make sure we get surface events
getHolder().addCallback(this);
// make sure that we support transparency
setZOrderOnTop(true);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.d("BrowserView", "surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
Log.d("BrowserView", "surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.d("BrowserView", "surfaceDestroyed");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Rect windowRect = new Rect();
getWindowVisibleDisplayFrame(windowRect);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = Math.min(MeasureSpec.getSize(heightMeasureSpec), windowRect.bottom);
setMeasuredDimension(width, height);
}
}
答案 0 :(得分:0)
在OnCreate函数中,在将BrowserView添加到相对布局中之后,将对setContentView(mRelativeLayout)的调用移动到函数的末尾。