Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android上的片段与活动画布绘制_Android_Android Fragments_Canvas - Fatal编程技术网

android上的片段与活动画布绘制

android上的片段与活动画布绘制,android,android-fragments,canvas,Android,Android Fragments,Canvas,我设计了一个surfaceview,它的画布是使用线程(游戏循环)绘制的。游戏循环绘制了300个小图像 如果我在活动上加载此surfaceview,则执行帧的时间比在片段上加载(使用活动)的时间短5-10毫秒 测试结果 300图像循环-活动(24毫秒),片段(32毫秒) 这背后的原因是什么?如何提高Fragment中的性能 这是我的密码。末端的循环是瓶颈。集群列表可能有300个项目要绘制 public List<PCluster> clusters = new ArrayList&l

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

如果我在活动上加载此surfaceview,则执行帧的时间比在片段上加载(使用活动)的时间短5-10毫秒

测试结果 300图像循环-活动(24毫秒),片段(32毫秒)

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

这是我的密码。末端的循环是瓶颈。集群列表可能有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);
    }
}
具有表面视图的游戏布局

<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" />


我认为这将大大增加你获得有意义答案的机会。你能发布一个功能齐全的示例吗?我认为使用自定义视图可以获得更好的结果,保持可重用性而不损失太多性能,但我不能完全确定。自定义视图是什么意思。我认为Surface View可以提高性能和UI体验。正因为如此,我才从普通画布上转到了surface view。你能用片段容器和片段布局发布你的xml吗?我现在已经添加了布局,谢谢
<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" />