2

在我的应用程序中,我有一个活动,当我按下菜单按钮时,它会在当前活动之上打开。在这个叠加层中,我希望我的视图在活动出现时淡入,并在它关闭之前再次淡出。这是我的代码:

public class OverlayActivity extends Activity {
TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.overlay);
    t = (TextView) findViewById(R.id.view_overlay_text);
    t.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean r = false;
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        finishOverlay();
        r = true;
        break;
    default:
        r = super.onKeyDown(keyCode, event);
        break;
    }
    return r;
}

private void finishOverlay() {
    Animation a = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    a.setAnimationListener(fadeOutComplete);
    t.setText("TEST"); // <--- if i add this line the code suddenly works
    t.setAnimation(a);
}

Animation.AnimationListener fadeOutComplete=new Animation.AnimationListener() {
    public void onAnimationEnd(Animation animation) {
        finish();
    }

    public void onAnimationRepeat(Animation animation) {
        // not needed
    }

    public void onAnimationStart(Animation animation) {
        // not needed
    }
};

}

不知何故,fadeOut-Animation 仅在我执行 t.setText("sometext") 之类的操作时才有效。如果我省略了那条线,它不会动画,因此 AnimationListener 不会被触发。

更新:更多信息确实可以解决问题: onCreate: TextView 淡入,我可以在屏幕上看到它 onKeyDown "BACK": finishOverlay 被调用。(它确实如此)finishOverlay:动画未应用于我想要淡入淡出的视图。为什么?是同一个参考。这可能是某种范围界定问题吗?

4

2 回答 2

0

您需要发布您的 textview 的 XML 代码。

我怀疑 textview 中的文本是空白的……因此没有动画发生。

另外,为什么要在代码中设置 textview 的可见性。由于 textview 出现在您的 onCreate 方法中,因此您不需要此行,因为您不应该在 XML 文件中将其设置为不可见。

于 2010-11-11T12:59:07.653 回答
0

我已经更新了您的代码,现在此代码可以按照您的意愿工作。

public class mp3list extends Activity {
TextView t;
Animation a;
Animation.AnimationListener fadeOutComplete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mp3list);
    t = (TextView) findViewById(R.id.tvc);  
    run();
    startOverlay();
   }

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean r = false;

switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
    run();
    finishOverlay();
    r = true;        
    break;
default:
    r = super.onKeyDown(keyCode, event);
    break;

}
return r;
}

private void startOverlay() {

a = AnimationUtils.loadAnimation(this, R.anim.fade_in);
a.setAnimationListener(fadeOutComplete);
t.setText("helo world"); // <--- if i add this line the code suddenly works
t.setAnimation(a);
}

private void finishOverlay() {
 a = AnimationUtils.loadAnimation(this, R.anim.fade_out);
 a.setAnimationListener(fadeOutComplete);
 t.setText("helo world"); // <--- if i add this line the code suddenly works
 t.setAnimation(a);
 finish();
}

public void run(){

    fadeOutComplete = new Animation.AnimationListener() {
        public void onAnimationEnd(Animation animation) {

        }

        public void onAnimationRepeat(Animation animation) {
            // not needed
        }

        public void onAnimationStart(Animation animation) {
            // not needed
        }
        };
    }

}    
于 2015-08-05T05:21:35.960 回答