Android和Engine-如何更新ChangeableText值

Android和Engine-如何更新ChangeableText值,android,text,andengine,Android,Text,Andengine,我对AndEngine中的可变文本有点问题。我想知道如何在不冻结屏幕的情况下更新它的文本?目前我正在使用这种方式,但它可能会将我的手机冻结2-3秒: private void removeFace(final Sprite face) { hm = getIconNames(); if(face.getUserData().equals("petrol")){ elapsedText.setText(hm.get(25));

我对AndEngine中的可变文本有点问题。我想知道如何在不冻结屏幕的情况下更新它的文本?目前我正在使用这种方式,但它可能会将我的手机冻结2-3秒:

    private void removeFace(final Sprite face) {

        hm = getIconNames();
        if(face.getUserData().equals("petrol")){

            elapsedText.setText(hm.get(25));           

            final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face);

            this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
            this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());
            this.mScene.unregisterTouchArea(face);
            this.mScene.detachChild(face);

        } else {

        }

        System.gc();
}

你知道怎么做吗?

这可能是因为你在设置文本时获取了一些信息

你应该做的是,把你的

String hm = hm.get(25); //What ever the correct object is or variable. im guessing its a string or int.
然后

将其传递给要设置的可变文本

elapsedText.setText(hm);   //If its a int make sure you do String.valueOf(hm);

这可能是因为您在设置文本时获取了一些信息

你应该做的是,把你的

String hm = hm.get(25); //What ever the correct object is or variable. im guessing its a string or int.
然后

将其传递给要设置的可变文本

elapsedText.setText(hm);   //If its a int make sure you do String.valueOf(hm);

这里仅有的3种可能需要较长时间的方法是
getIconNames()
get()
,以及
System.gc()

其他方法通常是立即返回的方法,或者具有非常低的复杂性。例如,
getPhysicConnectorManager()
会立即返回
findPhysicsConnectorByShape
unregisterPhysicsConnector
unregisterTouchArea
detachChild
都具有O(n)的复杂性,(其他大多数方法也具有O(1)或O(n)的复杂性)

我建议您查看LogCat,当调用
System.gc()
时,您将看到
Log.I
(蓝色)消息
dalvikvm
标记,该消息将以
gc\u EXPLICIT
开头,并将为您提供有关垃圾收集花费了多长时间的一些信息,等等


如果GC调用没有花费时间,那么它必须是您的两个方法,
getIconNames()
hm.get()
。您可以在每个代码行后面放一条Log.d消息,这将写入最后执行的代码行。通过这种方式,您可以跟上时代。

这里仅有的3种可能需要很长时间的方法是
getIconNames()
get()
,以及
System.gc()

其他方法通常是立即返回的方法,或者具有非常低的复杂性。例如,
getPhysicConnectorManager()
会立即返回
findPhysicsConnectorByShape
unregisterPhysicsConnector
unregisterTouchArea
detachChild
都具有O(n)的复杂性,(其他大多数方法也具有O(1)或O(n)的复杂性)

我建议您查看LogCat,当调用
System.gc()
时,您将看到
Log.I
(蓝色)消息
dalvikvm
标记,该消息将以
gc\u EXPLICIT
开头,并将为您提供有关垃圾收集花费了多长时间的一些信息,等等


如果GC调用没有花费时间,那么它必须是您的两个方法,
getIconNames()
hm.get()
。您可以在每个代码行后面放一条Log.d消息,这将写入最后执行的代码行。这样你就可以跟上时代。

记住,当你拆下孩子时,你应该在线程中这样做,因为如果你不这样做,它可能会导致错误。使用此结构

runOnUpdateThread(new Runnable(){

            @Override
            public void run() {
                if(yourSprite.hasParent())
                    scene.detachChild(yourSprite);
            }});
如果你想的话,你可以把所有的代码都放在那里,那么你的手机就不应该免费了

    private void removeFace(final Sprite face) {
        runOnUpdateThread(new Runnable(){
                @Override
                public void run() {
                hm = getIconNames();
                if(face.getUserData().equals("petrol")){

                    elapsedText.setText(hm.get(25));           

                    final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face);

                    this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
                    this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());
                    this.mScene.unregisterTouchArea(face);
                    this.mScene.detachChild(face);

                } else {

                }

                System.gc();
                }});


    }

请记住,当您拆离子线程时,您应该在线程中执行此操作,因为如果不执行此操作,可能会导致错误。使用此结构

runOnUpdateThread(new Runnable(){

            @Override
            public void run() {
                if(yourSprite.hasParent())
                    scene.detachChild(yourSprite);
            }});
如果你想的话,你可以把所有的代码都放在那里,那么你的手机就不应该免费了

    private void removeFace(final Sprite face) {
        runOnUpdateThread(new Runnable(){
                @Override
                public void run() {
                hm = getIconNames();
                if(face.getUserData().equals("petrol")){

                    elapsedText.setText(hm.get(25));           

                    final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face);

                    this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
                    this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());
                    this.mScene.unregisterTouchArea(face);
                    this.mScene.detachChild(face);

                } else {

                }

                System.gc();
                }});


    }