При использовании кода в классе текущей Activity все работает:
Animation anim;
anim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
view.startAnimation(anim);
Когда пытаюсь реализовать тот же код в отдельном классе, среда подчеркивает this
в параметрах loadAnimation
:
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);
}
}
Из документации Android: Метод: loadAnimation (Context context, int id). Parameters: context - Application context used to access resources
Таким образом сюда следует передать Application context. Но как это сделать? Пытался с помощью getApplicationContext(), но среда на это ругается тоже.
Context
в конструктор 2. получить Context изView v
во втором случае этоAnimationUtils.loadAnimation(v.getContext(), v);