I designed a surfaceview, where it's canvas is drawn using a thread (Game loop). The Game loop draws 300 small images.
If I load this surfaceview on activity, the time to execute a frame is 5- 10 ms lower than if I load it on a fragment (withing an activity).
Test results 300 images loop- activity (24 millsec) , Fragment (32 millsec).
What is the reason behind this? How can I improve the performance in Fragment?
Here is my code. The loop at the end is the bottleneck. clusters list could have 300 items to draw.
public List<PCluster> clusters = new ArrayList<PCluster>(); public void doDraw(Canvas canvas) { try { if (canvas == null) { return; } super.draw(canvas); canvas.drawColor(backColor); if (waitTillLoad) { return; } } catch (Exception ex) { } final int savedState = canvas.save(); try { minX = (int) (((viewWidth / mScaleFactor) - Gen.screenSizeWidth) / 2) * Utils.VIEW_SIZE; minY = (int) (((viewHeight / mScaleFactor) - Gen.screenSizeHeight) / 2) * Utils.VIEW_SIZE; if (mPosX > maxX) { mPosX = maxX; } if (mPosX < minX) { mPosX = minX; } if (mPosY > maxY) { mPosY = maxY; } if (mPosY < minY) { mPosY = minY; } canvas.scale(mScaleFactor, mScaleFactor); canvas.translate(mPosX, mPosY); long startTime = System.currentTimeMillis(); if (!showBackground) { for (PCluster cluster : clusters) { if (cluster.isVisible) { canvas.drawBitmap(cluster.Picture, cluster.BoardLocation.left, cluster.BoardLocation.top, null); } } Log.d("myApp", " " + (System.currentTimeMillis() - startTime)); } } catch (Exception ex) { } finally { canvas.restoreToCount(savedState); } }
This is main activity layout. @+id/fragment is used to load the layout below which has SurfaceView
LinearLayout
where the surface view is inserted at run time and where the drawing happens.
Main Layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/fragment_fake" android:layout_width="0dp" android:layout_height="0dp" android:gravity="center" android:orientation="vertical" android:layout_above="@+id/fragment"/> <LinearLayout android:id="@+id/fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:layout_above="@+id/fragment_ad"/> <fragment android:id="@+id/fragment_ad" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" class="controllers.AdFragment" /> </RelativeLayout>
Game Layout with surface view
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/surfaceView" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> <include android:id="@+id/gamesettingpanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" layout="@layout/gameoption_panel" /> <include android:id="@+id/scorepanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:gravity="center_horizontal" layout="@layout/scorepanel" /> <include android:id="@+id/chaetpanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" layout="@layout/gamecheat_panel" /> <include android:id="@+id/finishpanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:gravity="center_horizontal" android:layout_alignParentTop="true" layout="@layout/finishedpanel" />
0 comments:
Post a Comment