Snippet versus drawing activity canvas on android
I developed an overview of the surface where his canvas is drawn using a stream (game loop). The game loop draws 300 small images.
If I load this surface view onto an activity, the frame execution time is less than 5-10ms if I load it onto a fragment (with an activity).
Results of testing 300 images of loop-activity (24 millisec), fragment (32 milliseconds).
What is the reason for this? How to improve performance in Fragment?
Here is my code. The loop at the end is the bottleneck. the cluster list can contain 300 items.
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 the main layout. @ + id / fragment is used to load the layout below, which has SurfaceView
LinearLayout
where the surface view is inserted at runtime and where the drawing is executed.
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" />
source to share
No one has answered this question yet
Check out similar questions: