【问题标题】:Android AndEngine - How can I update ChangeableText valueAndroid AndEngine - 如何更新 ChangeableText 值
【发布时间】:2011-12-15 21:35:37
【问题描述】:

我对 AndEngine 中的 ChangeableText 有一点问题。我想知道如何在不冻结屏幕的情况下更新它的文本?目前我正在使用这种方式,但它可能会冻结我的手机 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();
}

有什么办法吗?

【问题讨论】:

    标签: android text andengine


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      你应该做的是,得到你的

      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】:

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

        其他的通常是立即返回的方法,或者具有非常低的复杂性。例如,getPhysicsConnectorManager() 立即返回。 findPhysicsConnectorByShapeunregisterPhysicsConnectorunregisterTouchAreadetachChild 的复杂度都是 O(n),(并且大多数其他方法的复杂度也是 O(1) 或 O(n))。

        我建议你查看 LogCat,当调用System.gc() 时,你会看到dalvikvm 标签的Log.i(蓝色)消息,该消息将以GC_EXPLICIT 开头,并会给你一些信息关于垃圾收集花了多长时间等......

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多