2

So I'm trying to do some animations in my app and had a way to do it without using ObjectAnimator and AnimatorListener from API 11. However, they're not as smooth and robust as just doing animations using those classes.

So I made the changes to my code in my object's class with a check for the API level, so that ObjectAnimator and AnimatorListener stuff wouldn't be called unless the API level is 11+.

The problem is that my object is implementing AnimatorListener even though its functionality isn't being used in all versions of the code. I think this is leading to an VerifyError on start up of my app because now it crashes on devices with API level 10 and down.

Is there a way to conditionally implement an interface based on the API level or a different way to achieve the same thing?

Thanks!

corgichu
  • 2,580
  • 3
  • 32
  • 46

4 Answers4

3

You can try using https://github.com/JakeWharton/NineOldAndroids/ which backports the animation UI to pre-11.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
3

actually, it is OK if you build against or with a higher SDK target, the issue is only when your code is really run on a device missing classes from a higher SDK. As others suggested, you can go for a factory

public class Factory {
  public static <T> T getImplementation(){
    if(<SDK_LEVEL_INCOMPATIBLE>){
      return (T)new <package>.OldSchoolAnimator();
    }else{
      return (T)new <package>.SuperAnimator();
    }
  }
}


...
SomeImplementation impl = new Factory().getImplementation();
...

watchout, "SomeImplementation" has to be a common super-type or interface of OldSchoolAnimator and SuperAnimator classes! Dont use "imports" for implementation classes in your factory, rather use full-qualified classnames.

comeGetSome
  • 1,913
  • 19
  • 20
2

I think you are looking for the factory design pattern:

http://en.wikipedia.org/wiki/Factory_method_pattern

jan
  • 3,923
  • 9
  • 38
  • 78
0

I ended up using an anonymous inner class for the listener and used the full-qualified classnames instead of importing the other animation objects!

corgichu
  • 2,580
  • 3
  • 32
  • 46