Reputation: 2600
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!
Upvotes: 2
Views: 1243
Reputation: 2600
I ended up using an anonymous inner class for the listener and used the full-qualified classnames instead of importing the other animation objects!
Upvotes: 0
Reputation: 1963
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.
Upvotes: 3
Reputation: 4103
I think you are looking for the factory design pattern:
http://en.wikipedia.org/wiki/Factory_method_pattern
Upvotes: 2
Reputation: 93559
You can try using https://github.com/JakeWharton/NineOldAndroids/ which backports the animation UI to pre-11.
Upvotes: 3