How can I programmatically translate an arbitrary view without using an animation (android.view.animation.*
)?
API 11 introduced setTranslationX
Grishka's answer helped me a lot, works perfectly. In order to animate the translation using the Honeycomb API with nineoldandroids, I set up a FloatEvaluator for the translation value and then used either setTranslationX on Honeycomb+ or the TranslateAnimation method. Here is my code:
public class TranslationXEvaluator extends FloatEvaluator {
private View view;
public TranslationXEvaluator(View view) {
this.view = view;
}
@Override
public Float evaluate(float fraction, Number startValue, Number endValue) {
float translationX = super.evaluate(fraction, startValue, endValue);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
view.setTranslationX(translationX);
} else {
TranslateAnimation anim = new TranslateAnimation(translationX, translationX, 0, 0);
anim.setFillAfter(true);
anim.setDuration(0);
view.startAnimation(anim);
}
return translationX;
}
}