fhucho
fhucho

Reputation: 34560

Animate View in a layout (pre-Honeycomb)

I tried to animate View's position in a RelativeLayout. In every frame I change the topMargin of the View's LayoutParams and then call View#setLayoutParams(newParams).

The animation is unfortunately too slow because the setLayoutParams() call triggers remeasuring of the whole View tree.

How can I solve this without using API 11+?

Upvotes: 2

Views: 705

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

You can use View Animations Just make yourself an xml file with a translate animation in it that defines how you'd like your View to move. Then inflate a reference to your Animation and call

view.startAnimation(anim);

Here is an example of a translate xml file. You'd save a file like this in res/anim/

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/overshoot_interpolator">

<translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="700"/>


</set>

Here is what you'd do in java to use it.

Animation slideLeftIn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
    //mView can be any View object.
    mView.startAnimation(slideLeftIn);
}

Upvotes: 2

Related Questions