0

I have to implement a (child) layout translate animation programmatically because the distance to translate is determined at runtime. I have set the following translate animation

    TranslateAnimation tanim = new TranslateAnimation(
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 100);

to slide down the layout, for example.

Problem 1: This layout will snap back to its original position after the animation finishes. Is there anything I should set to maintain its position? I suspect this is caused by the layout being moved out of the parent layout (parent layout has parameter layout_width="wrap_content"). If this is the case how can I adjust the parent layout to accomodate the child layout change?

Problem 2: Do I have to provide a custom interpolator class to achieve a decelerating effect? If yes, do you know where can I find an example? In xml I can do this

android:interpolator="@android:anim/accelerate_interpolator"

Is there an equivalent Android code to achieve the accelerate/decelerate effect programmatically?

4

1 回答 1

2

要在动画之后保持位置,您需要使用 fillAfter 属性。是的,公共 API 中有 Accelerate 和 Decelerate 插值器类

TranslateAnimation tanim = new TranslateAnimation(
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 0.0f,
        TranslateAnimation.ABSOLUTE, 100);
tanim.setFillAfter(true);
tanim.setInterpolater(new DecelerateInterpolator());
//tanim.setInterpolater(new AccelerateInterpolator());

有关 fillAfter 的信息,请参阅动画文档,有关系统中内置的 Interpolator 对象的列表,请参阅Interpolator 文档

于 2013-05-04T03:04:39.907 回答