Programmatically Translate View Pre-Honeycomb

How can I programmatically translate an arbitrary view without using an animation (android.view.animation.*)?

API 11 introduced setTranslationX and setTranslationY, setX and setY, and getMatrix—all of which can be used to accomplish what I'm looking for. I cannot seem to find an equivalent on the prior API levels, however.

The android.view.animation.TranslateAnimation uses getMatrix in its implementation (as far back as API 4) but it was a private API throughout this time.

Is there any way to accomplish this without resorting to reflection?


Solution 1:

Favorited because I would love to be proven wrong on this one, but I ran into a similar wall when doing some Drag-n-Drop investigations awhile back.

I'm pretty sure you're stuck with offsetTopAndBottom() and offsetLeftAndRight() for moving a View around without an animation in early APIs. The big downside here is that these methods actually modify the top/bottom/left/right values, so you have to do some pretty heavy tracking if you want to go back to where you started from.

Another option would be to simply use a TranslateAnimation with a VERY short duration and set to fillAfter...

Other options require subclassing, and still aren't pretty, like translating the Canvas the View draws on. The problem here is you also have to create a parent that doesn't clip it's children so the view can draw outside its own bounds.

Solution 2:

Yes, you can use TranslateAnimation. With fillAfter set to true your view will stay translated even after the animation ends. Here is an example:

TranslateAnimation anim=new TranslateAnimation(0, 0, 20, 20);
anim.setFillAfter(true);
anim.setDuration(0);
yourView.startAnimation(anim);

Solution 3:

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;
}

}

Solution 4:

I was having the same problem. I was able to achieve the translation by manipulating the layoutparams, specifically the margins, of the views I wanted to translate. You can use negative values for the margins btw.