【问题标题】:Animate View before closing Activity关闭 Activity 之前的动画视图
【发布时间】:2011-05-08 10:11:57
【问题描述】:

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

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:动画未应用于我想要淡入淡出的视图。为什么?是同一个参考。这可能是某种范围界定问题吗?

【问题讨论】:

    标签: android view translate-animation


    【解决方案1】:

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

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

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

    【讨论】:

    • 确实如此。我不必设置可见性。那是错误的,但我没有改变问题。文本视图不是空白的。我用imageview尝试了同样的事情。也不起作用。所以它对 textview 来说并不特殊。
    • 我想我会开始看看你为什么试图“抓住”后退按钮。一般来说,我见过人们使用 onDestroy() 进行活动。
    【解决方案2】:

    我已经更新了你的代码,现在这个代码和你希望的一样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多