我有一个游戏应用程序,其视图结构如下。首先,我有一个空的FrameLayout,如下所示:
FrameLayout game_frame_layout = new FrameLayout(getApplicationContext());
然后我像这样为其添加了两个视图:
game_frame_layout.addView(customView);
game_frame_layout.addView(butView);
customView用于显示各种移动游戏图形,而butView在移动图形之上显示一些ImageButton。CustomView是一个扩展SurfaceView类的CustomView类的实例。
CustomView包括以下代码
void updateView()
{
final SurfaceHolder holder = getHolder();
holder.setFormat(PixelFormat.RGBA_8888);
try
{
Canvas canvas = holder.lockCanvas();
if (canvas != null)
{
onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
holder.setFormat(PixelFormat.RGBA_8888);
这一行是最近添加的(参见这里)。如果没有这行代码,我的动画图形会出现颜色太少的格式(通过实验我推断出它是“RGB_565”),因此我会得到一些条纹状伪影。当我添加了setFormat这行代码后,图形在我的三星Galaxy Tab 10.1(Android 3.1)上显示得非常完美(没有条纹),但在另外三个设备上却不行:三星GT-l9100(4.1.2)、Nexus 7 ME370T 4.4.2和HTC One X 4.2.2,我只能看到对应于butView的按钮,而整个背景都是黑色。日志中没有任何程序崩溃的迹象。
有任何想法吗?
PixelFormat.RGB_888
吗? - tiguchi