English 中文(简体)
Android: LinearLayout addView Animation
原标题:

I currently have a working Android program that programmatically adds views to a LinearLayout. I would like those views to be animated in and cannot find any good resources on figuring out how to do this.

Could someone point me in the right direction?

问题回答

It s a very old question, but still interesting: you can use the attribute android:animateLayoutChanges="true"

For example:

<LinearLayout

                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
               />

That can be done with Scenes and Transitions API.

Framework gives us three Transition types out of the box: Fade, Slide and Explode, but you can also create your custom type of transition extending Visibility class and overriding appropriate methods.

So, having any ViewGroup, we can do this:

viewGroup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        Transition t = null;
        if (i == 1) {
            t = new Fade();
        } else if (i == 2) {
            t = new Slide(Gravity.BOTTOM);
        } else if (i == 3) {
            t = TransitionInflater.from(v.getContext())
                                  .inflateTransition(R.transition.my_transition);
        }

        Button button = new Button(v.getContext());
        button.setText("My button " + i++);

        TransitionManager.beginDelayedTransition(customLayout, t);
        viewGroup.addView(button);
    }
});

Where my_transition.xml is following:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
               android:duration="3000"
               android:interpolator="@android:interpolator/fast_out_slow_in">

    <fade/>
    <slide android:slideEdge="bottom"/>

</transitionSet>

We ll get this result:

enter image description here

Note, that we have to perform TransitionManager.beginDelayedTransition() before any change to layout has been made (i.e. before addView() is called). Then framework will take care of the rest.

There is also another overload TransitionManager.beginDelayedTransition(ViewGroup), where you do not need to specify what exact transition you want to be applied, and system will perform AutoTransition animation, which basically will fade and change boundaries of animated view.


Update sum up from conversation in comments

Framework s TransitionManager is available from API 19, and fully supported from API 21 (by saying fully I mean e.g. Slide transition is available from API 21). Although there is support package available, but it doesn t backport all the functionality. Alternatively, you can move to TransitionsEverywhere library, which backports everything up to Android 4.0.

Try

1. Add view to linear layout

linearLayout.addView(customView);

2. Add slide_up.xml to res/anim folder

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">

  <translate
    android:duration="500"
    android:fromYDelta="100%"
    android:toYDelta="0%" />
</set>

3. Apply animation right after adding view

Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_up);
customView.startAnimation(animation);

If you want to animate views one by one, then use following lines

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            linearLayout.addView(customView);
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_up);
           customView.startAnimation(animation);
        }
 }, 500);

You can use ViewFlipper and set animations from there, you can take a look at this tutorial . Good luck.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签