Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View animate going behind other layout

I'm trying to animate a view to another layout, but i got an issue, because my view is behind the layout. I tried put android:animateLayoutChanges="true", but without success.
My code:
layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">
    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:animateLayoutChanges="true"
        android:background="#ffffff">

    </LinearLayout>
</RelativeLayout>

My activicty class:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        float x = 300;
        float y = 300;
        RadioButton button = (RadioButton) findViewById(R.id.radioButton);
        button.animate().setDuration(10000).x(x).y(y);
    }
}
like image 788
user1851366 Avatar asked Aug 01 '15 16:08

user1851366


People also ask

What is animateLayoutChanges?

Recently, I have discovered a new one regarding android:animateLayoutChanges . For those unfamiliar with this XML attribute, it's a “automagical” way to animate changes in your ViewGroups. This has been around since API 11 aka Honeycomb.

How to add animation to layout in android?

All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you. Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition() method.

How do you animate a view?

Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.


1 Answers

The items will be drawn in the same order as they are ordered in the layout. For your layout, the RadioButton will be draw first and then the LinearLayout. If they are overlapping, the LinearLayout will be drawn over the RadioButton.

Solution Part #1: Flip the order of RadioButton and LinearLayout so that the RadioButton is always drawn last.

<RelativeLayout
    android:id="@+id/final_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:background="#ffffff">
</RelativeLayout>

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton" />

A more rigorous explanation on the Android Developer's site.

Solution Part #2: Unrelated to the point I made above but probably equally important, you need to start the animator. Updated source below:

public class MainActivity extends AppCompatActivity {

    private RadioButton mRadioButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioButton = (RadioButton) findViewById(R.id.radioButton);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        float x = 300;
        float y = 300;

        ViewPropertyAnimator animator = mRadioButton.animate().setDuration(10000).x(x).y(y);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {}
            @Override
            public void onAnimationEnd(Animator animation) {
                Log.d(TAG, "onAnimationEnd");
            }

            @Override
            public void onAnimationCancel(Animator animation) {}
            @Override
            public void onAnimationRepeat(Animator animation) {}
        })
        animator.start();
    }

    private static final String TAG = MainActivity.class.getSimpleName();
}

Tested, animates on app startup in Android 5.1.

like image 133
Trevor Carothers Avatar answered Oct 10 '22 21:10

Trevor Carothers