开发者

Animate View before closing Activity

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?


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.

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

}    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜