When using code in the class of the current Activity, everything works:

Animation anim; anim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim); view.startAnimation(anim); 

When I try to implement the same code in a separate class, the environment emphasizes this in the loadAnimation parameters:

 class CustomAnimation implements Animation.AnimationListener { View v; void animation () { Animation anim; anim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim); anim.setAnimationListener(this); v.startAnimation(anim); } @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { v.setVisibility(View.VISIBLE); } @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); } } 

From the Android documentation: Method: loadAnimation (Context context, int id). Parameters: context - Application context used to access resources

Thus, the application context should be passed here. But how to do that? I tried using getApplicationContext (), but the environment curses it, too.

  • 2
    1. transfer the Context to the constructor 2. get the Context from View v in the second case, this is AnimationUtils.loadAnimation(v.getContext(), v); - SorryForMyEnglish
  • @SorryForMyEnglish pass the Context to the constructor - tell me how to implement this correctly? - Doraemon

2 answers 2

 class CustomAnimation implements Animation.AnimationListener { private Context context; public CustomAnimation(Context context){ this.context = context; } void animation () { Animation anim; anim = AnimationUtils.loadAnimation(context, R.anim.alpha_anim); anim.setAnimationListener(this); v.startAnimation(anim); } ... } 

Just do not understand how you initialize v you probably have not a full listing of the class.

I think the final version will be

 class CustomAnimation implements Animation.AnimationListener { private Context context; private View animatedView; public CustomAnimation(Context context, View view){ this.context = context; this.animatedView = view; } void animation () { Animation anim; anim = AnimationUtils.loadAnimation(context, R.anim.alpha_anim); anim.setAnimationListener(this); animatedView.startAnimation(anim); } ... } 

When creating an object of type CustomAnimation transfer the context and view you want to animate. If you work in activation, you can pass this to the constructor, if you get the context from getActivity() in the fragment In general, give us more code and then you will be answered unequivocally!

  • Accidentally deleted the initialization v from the class, returned. - Doraemon
  • I still do not see initialization. I see View v; and what, you will have an exception at runtime NP - SorryForMyEnglish
  • was going to assign the v link in an instance of this class - Doraemon
  • I recommend that you use primitive encapsulation techniques right away at the beginning of your career :-) - SorryForMyEnglish
  • At the beginning of my career I can not catch what you mean?) - Doraemon

for example, you can pass this:

 class CustomAnimation ................... { Context ctx; CustomAnimation(Context ctx) { this.ctx = ctx; } void animation () { Animation anim; anim = AnimationUtils.loadAnimation(this.ctx, R.anim.alpha_anim); anim.setAnimationListener(this); v.startAnimation(anim); } ........ };