片段与活动画布画在android上

时间:2017-03-24 11:34:41

标签: android android-fragments canvas

我设计了一个surfaceview,它使用一个线程(游戏循环)绘制画布。游戏循环绘制300个小图像。

如果我在活动上加载此surfaceview,则执行帧的时间比将其加载到片段(带有活动)时低5到10毫秒。

测试结果 300个图像循环活动(24毫秒),碎片(32毫秒)。

这背后的原因是什么?如何提高Fragment的性能?

这是我的代码。最后的循环是瓶颈。 clusters 列表可以包含300个项目。

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);
    }
}

这是主要的活动布局。 @ + id / fragment用于加载下面的布局,其中SurfaceView LinearLayout在运行时插入表面视图,并且绘图发生的位置。

主要布局

<?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>

带有表面视图的游戏布局

<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 个答案:

没有答案
相关问题