问题
I have code like this:
else if (v == mSettings)
{
if (disappearView.getVisibility() == View.VISIBLE)
{
AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
fadeOutAnimation.setDuration(1000); // time for animation in milliseconds
fadeOutAnimation.setFillAfter(true); // make the transformation persist
Animation out = AnimationUtils.makeOutAnimation(this, true);
disappearView.startAnimation(out);
disappearView.setVisibility(View.INVISIBLE);
out.setAnimationListener(new Animation.AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
disappearView.setVisibility(View.GONE);
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
bannerView.startAnimation(in);
bannerView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationStart(Animation animation) { }
});
}
else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
disappearView.startAnimation(in);
disappearView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.INVISIBLE);
bannerView.setVisibility(View.GONE);
}
}
It all works apart from this section in the animationListener:
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
It wants a valid context but it is receiving an animationListener, what context do I give it, context really confuses me here.
回答1:
Change it to:
Animation in = AnimationUtils.loadAnimation(MyActivityName.this, android.R.anim.fade_in);
It wants an instance of any class that extends Context. Since it is inside an anonymous inner class, when you use this
you are referring to the inner class instance, and not to your Activity. My snippet refers to the Activity class that wraps the anonymous inner class. Since Activity extends Context, this is a valid argument.
回答2:
Inside the listener, this
is referring to the Listener
. Try using ActivityName.this
来源:https://stackoverflow.com/questions/15226521/how-to-use-context-inside-listener