Reputation: 8972
i'm currently writing a game and now encountered a issue.
in my game, if i overlay a framelayout over my custom surfaceview class, my frame rate will drop according to how many view widgets are placed in the framwlayout.
i'm using the framelayout to place some game controls and textviews for point display..
i thought about implementing the controls directly on the surfaceview in the gameloop but that basically destroys the purpose of Android UI widgets plus it would be much easier to must move around the XML.
how to solve this problem?
here is my code: XML file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.sora.game.npc.Panel
android:id="@+id/myview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="179dp"
android:columnCount="8"
android:rowCount="5" android:layout_gravity="bottom|left" android:visibility="visible">
<Button
android:id="@+id/upleft"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_column="0"
android:layout_gravity="bottom|right"
android:layout_row="0"
android:text="↖" />
<Button
android:id="@+id/up"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="bottom"
android:layout_row="0"
android:text="↑" />
<Button
android:id="@+id/upright"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_column="2"
android:layout_columnSpan="4"
android:layout_gravity="bottom"
android:layout_row="0"
android:text="↗" />
<Button
android:id="@+id/left"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_gravity="bottom"
android:layout_row="1"
android:text="←" />
<Button
android:id="@+id/vdp"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="bottom"
android:layout_row="1"
android:text="⥁" />
<Button
android:id="@+id/right"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="2"
android:layout_columnSpan="4"
android:layout_gravity="left"
android:layout_row="1"
android:text="→" />
<Button
android:id="@+id/downleft"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_column="0"
android:layout_gravity="right"
android:layout_row="2"
android:text="↙" />
<Button
android:id="@+id/down"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_gravity="left"
android:layout_row="2"
android:text="↓" />
<Button
android:id="@+id/downright"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_column="2"
android:layout_columnSpan="4"
android:layout_gravity="left"
android:layout_row="2"
android:text="↘" />
<Space
android:layout_width="1dp"
android:layout_height="47dp"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="1" />
<Space
android:layout_width="61dp"
android:layout_height="1dp"
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
<Space
android:layout_width="30dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
</GridLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawingCacheQuality="high"
android:visibility="visible" >
<TextView
android:id="@+id/life"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Life"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Score"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/npcqt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="NPC quantity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/strike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/score"
android:layout_marginTop="67dp"
android:text="strike" />
<Button
android:id="@+id/fire"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="----Fire!----"/>
<Button
android:id="@+id/cycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/strike"
android:text="cycle" />
</RelativeLayout>
</FrameLayout>
Edit: i've made some experiments:
if i scale my custom surface view to a size that no UI widgets are on top(overlaying) the surfaceview, the lag and drop of FPS does not occur, which makes me think the problem is no rendering related....
Upvotes: 0
Views: 1797
Reputation: 6929
I don't think that there is an easy solution. Just to be sure i would first use TraceView to check where the performance bottleneck really is. Now if this bottleneck is indeed related to the Android Widgets (it most probably is since combining SurfaceViews with Android Widgets is known to be costly) you could try to do this.
getWindow().setFormat(PixelFormat.RGB_565);
holder.setFormat(PixelFormat.RGB_565);
this might help with the performance of the surface compositing but you will lose alpha. Also make sure that your sprites have the same format. If this doesn't help you would either need to use less Widgets or start drawing you own widgets on the canvas. But be sure to check out Game Engines like AndEngine. No need to reinvent the wheel for basic tasks :-)
Another Idea: Do you see the performance problems on all devices or only on tablets? In my personal experience tablets are much more sensitive to performance problems because their pixel count is higher in comparison to their gpu power. So have you activated hardware acceleration in the manifest.xml?
Upvotes: 1