Sunday, 11 December 2016

Android Animations: Expand & Collapse

In reference to my previous post here, this post is regarding some other styles of animation that you can use in your app to make it look more subtle.
As you know Animations in android can add subtle visual cues that notify users about what's going on in your app and improve their mental model of your app's interface. Animations are especially useful when the screen changes state, such as when content loads or new actions become available. Animations can also add a polished look to your app, which gives your app a higher quality feel.

So lets get started now: This tutorial shows you simple way to add Expand & Collapse style animation in your app in just a few steps.
The Expand animation is somewhat like unfolding a view step by step or you can say opening a view from 0 to 100 slowly.
So to add the expand animation in your app just add the following method in your code:

public static void expandAnimation(final View v) {
        v.measure(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? WindowManager.LayoutParams.WRAP_CONTENT
                        : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        // 1dp/ms
        a.setDuration(300);
        v.startAnimation(a);
    }
          

Now to set this animation on any view, just call this method and pass that view which you want to expand, as the parameter to this method as follows:

                    expandAnimation(findViewById(R.id.myview));       

Now after expanding the view you also need to close it or you can say you'll need to collapse it.
Don't worry, closing it is also as simple as expanding it.
Just add the following method in your code to collapse your view.    

public static void collapseAnimation(final View v) {
        final int initialHeight = v.getMeasuredHeight();
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    v.setVisibility(View.GONE);
                } else {
                    v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        // 1dp/ms
        a.setDuration(300);
        v.startAnimation(a);
    }
        

Now to set this collapse animation on any view, just call this method as you called the method to expand it and pass that view which you want to collapse, as the parameter to this method as follows:

                       collapseAnimation(findViewById(R.id.myview));    

That's all. Use it and have fun.
Comment if any issues and share.



12 comments:

  1. MediaFire APK is a versatile app that lets users upload, store, and share files easily on mobile devices. Its intuitive interface makes file management simple and fast. To access files without an internet connection, follow the mediafire apk offline mode guide for step-by-step instructions. The app also supports secure storage, quick uploads, and easy sharing links.

    ReplyDelete
  2. Pak14 game is an online gaming platform designed to provide users with an engaging and convenient gaming experience. Its simple interface makes it easy for players to explore available games, navigate different sections, and enjoy entertainment from their mobile devices. Users should review the platform’s terms, privacy information, and applicable local regulations before using any gaming or real-money features. For a smoother experience, it is also recommended to use a secure internet connection and download applications only from trusted sources.

    ReplyDelete
  3. PelisPlus TV is a popular streaming platform known for providing access to a wide selection of movies and TV shows in one convenient place. Its simple interface makes it easy for users to browse different genres, discover new titles, and find entertainment that matches their interests. Viewers should always check the legality and availability of content in their region and use authorized streaming services whenever possible. For a safe viewing experience, it is also important to avoid suspicious downloads, pop-ups, and unofficial apps that may pose privacy or security risks.

    ReplyDelete
  4. shark simulator game offers an exciting underwater adventure where players take control of a powerful shark and explore a vast ocean environment. Players can swim through different areas, hunt for food, complete challenges, and encounter various sea creatures while trying to survive and grow stronger. With engaging gameplay, simple controls, and an open-world style environment, the game provides an entertaining experience for players who enjoy action and marine-themed simulation games.

    ReplyDelete
  5. TheFreeCine moviesTheFreeCine movies to discover and watch movies and other entertainment content online. Users may search for TheFreeCine movies to explore different genres, popular titles, and recently released content from one place. When choosing a streaming platform, it is important to consider video quality, device compatibility, privacy, and whether the content is available through legitimate licensing. For a reliable viewing experience, audiences should prefer authorized streaming services that provide movies legally and securely.

    ReplyDelete
  6. FreeCine Updated Version offers users a convenient way to explore movies, TV shows, and other entertainment content through a simple and user-friendly interface. The updated version may include performance improvements, smoother navigation, bug fixes, and enhanced compatibility with newer devices. Users looking for a modern streaming experience can explore its features and check that they are using a legitimate and safe version from a trusted source.

    ReplyDelete
  7. Terraria latest version brings players an exciting sandbox adventure filled with exploration, crafting, building, combat, and discovery. Players can explore diverse worlds, gather resources, create powerful equipment, construct unique bases, and battle challenging enemies and bosses. The latest version also offers improvements, new content, and gameplay enhancements that make the experience smoother and more enjoyable. Whether playing alone or with friends, Terraria provides endless opportunities to explore, create, and survive in a constantly changing world.

    ReplyDelete
  8. Cinema APK latest version offers a convenient way for Android users to explore movies and TV series through a simple, user-friendly interface. The app is designed to organize entertainment content by genres and provide features such as search, subtitles, and different playback options. However, users should verify the source and version before installing any APK, as unofficial streaming applications can carry security, privacy, or copyright concerns. For safer viewing, it is best to use legitimate streaming platforms and download apps from trusted sources.

    ReplyDelete
  9. 7989 game is a mobile gaming platform designed for players who enjoy a variety of interactive games in one place. It offers a simple interface, smooth navigation, and convenient access to different gaming options. Players can explore available features, manage their accounts, and enjoy gameplay from compatible mobile devices. Before using the platform, users should check the official version, supported devices, terms, and local regulations to ensure a safe and appropriate gaming experience.

    ReplyDelete
  10. 77987 game 987 Ga is a mobile gaming platform designed for users looking for a variety of entertaining games in one place. Its simple interface makes it easy to explore available game options, while smooth navigation helps provide a convenient user experience. Players can check the latest 7987 Game updates, features, and app improvements to stay informed about new additions. Before downloading or using the platform, users should verify the app source and review its terms and requirements.

    ReplyDelete
  11. 2jbet game is an online gaming platform that offers a variety of entertaining games through a simple and accessible interface. Players can explore different game categories, check available features, and navigate the platform from compatible devices. The platform is designed to provide a convenient gaming experience with easy account access and straightforward menus. Users should review the platform’s terms, age requirements, and local laws before using any real-money gaming features.

    ReplyDelete
  12. Y71 Game is an online gaming platform designed for players who enjoy a variety of entertaining games through a simple and accessible interface. The platform focuses on smooth navigation, quick access to different game categories, and a mobile-friendly experience. Users can explore available features, check game details, and enjoy gameplay from compatible devices. Before using any online gaming service, players should review its terms, age requirements, and applicable local regulations.

    ReplyDelete