Programmatically Translate View Pre-Honeycomb

后端 未结 7 1616
借酒劲吻你
借酒劲吻你 2021-01-30 00:09

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

API 11 introduced setTranslationX

相关标签:
7条回答
  • 2021-01-30 01:01

    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;
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题