Skip to content Skip to sidebar Skip to footer

Conditionally Implement An Interface

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 jus

Solution 1:

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

publicclassFactory {
  publicstatic <T> T getImplementation(){
    if(<SDK_LEVEL_INCOMPATIBLE>){
      return (T)new <package>.OldSchoolAnimator();
    }else{
      return (T)new <package>.SuperAnimator();
    }
  }
}


...
SomeImplementationimpl=newFactory().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.

Solution 2:

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

Solution 3:

Solution 4:

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

Post a Comment for "Conditionally Implement An Interface"