Sanyasirao Mopada February 2016

android swapping two views using animation when click on imagebutton

I have seen the questions in the stack over flow, even I tried,

Android Translate Animation like Swapping Two Views

But nothing worked for my scenario, I want to swap swap_above linear layout and swap_below linear layouts when click on swap image button. If possible I would like to apply animation for swap imagebutton also, when the views are being swapped.

Thank you,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/from"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingBottom="25dp"
                android:paddingLeft="25dp"
                android:paddingRight="25dp"
                android:paddingTop="15dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="From"
                    android:textStyle="bold" />

                <LinearLayout
                    android:id="@+id/swap_above"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:id="@+id/        

Answers


Swas_99 February 2016

First - set the below xml attributes for each target view's parent which may be blocking the animation :

    android:clipChildren="false"
    android:clipToPadding="false"

You need to set this attributes at 3 places in your case.
Read more about clipChildren & clipToPadding here - http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:clipChildren http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:clipToPadding

If your minSDK >=12
Use this for swap animation :

    findViewById(R.id.swap).setOnClickListener(new View.OnClickListener() {
        boolean isAnimating;
        @Override
        public void onClick(View v) {
            if(isAnimating)
                return;
            isAnimating=true;

            View v1 = findViewById(R.id.swap_above);
            View v2 = findViewById(R.id.swap_below);

           float x1, y1, x2, y2;
           x1 = getRelativeX(v1);//Use v1.getX() if v1 & v2 have same parent
           y1 = getRelativeY(v1);//Use v1.getY() if v1 & v2 have same parent
           x2 = getRelativeX(v2);//Use v2.getX() if v1 & v2 have same parent
           y2 = getRelativeY(v2);//Use v2.getY() if v1 & v2 have same parent

            float x_displacement = (x2-x1);
            float y_displacement = (y2-y1);

            v1.animate().xBy(x_displacement).yBy(y_displacement);
            v2.animate().xBy(-x_displacement).yBy(-y_displacement);
            long anim_duration = v1.animate().getDuration();

            //Wait till animation is over to set isAnimating to false
            //take 10 ms as buffer time to ensure proper functioning
            //If you remove t 

Post Status

Asked in February 2016
Viewed 2,319 times
Voted 7
Answered 1 times

Search




Leave an answer