在关闭 Activity 之前对视图进行动画处理
在我的应用程序中,我有一个活动,当我按下菜单按钮时,该活动会在当前活动之上打开。 在此叠加层中,我希望我的视图在活动出现时淡入,并在活动关闭之前再次淡出。 这是我的代码:
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:动画未应用于我想要淡入淡出的视图。为什么?这是相同的参考。这可能是某种范围界定问题吗?
in my application i have a activity which opens ontop of the current activity when i press the menu button.
In this overlay i want my views to fade in when the activity appears and fade them out again before it closes.
Here is my code:
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
}
};
}
Somehow the fadeOut-Animation only works if i do something like t.setText("sometext"). If i leave out that line it does not animate and therefore the AnimationListener ist not triggered.
UPDATE:
Some more Information do clearify the problem:
onCreate: TextView fades in and i can see it on the screen
onKeyDown "BACK": finishOverlay is called. (it actually does)
finishOverlay: Animation is not applied to the view i want to fade. Why? It's the same reference. Could it be a scoping Problem of some kind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要发布您的textview 的XML 代码。
我怀疑文本视图中的文本是空白的......因此没有动画发生。
另外,为什么要在代码中设置文本视图的可见性。由于 textview 出现在您的 onCreate 方法中,因此您不需要此行,因为您不应该将其在 XML 文件中设置为不可见。
You need to post your textview's XML code.
I suspect that the text in the textview is blank...thus no animation occurs.
Also, why are you setting the visibility of the textview in your code. Since the textview appears in your onCreate method, you don't need this line since you shouldn't have it set as invisible in your XML file.
我已更新您的代码,现在该代码的工作方式与您希望的相同。
I have Update your code and now this code is working same as you wish.