Animation on layout_gravity

In Android, is it possible to apply animation on layout_gravity? for example, suppose I want to change layout_gravityfor View(for example Button) from right to left

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="right|top" />
</FrameLayout>

In the above layout file, I want to scale the placement from right|topto left|topat runtime with animation, is it even possible? Thanks

+4
source share
2 answers

LayoutTransition ViewGroup, , , . , ( "+" "x" ).

animateLayoutChanges true :

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pnlFabContainer"
    android:animateLayoutChanges="true"
    android:clipChildren="false">

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:id="@+id/lstWhatever"
        android:divider="@color/DividerDarkGrey"
        android:dividerHeight="1dp"
        android:choiceMode="singleChoice"
        android:groupIndicator="@null"
        android:padding="20dp"/>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fabMultiSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="24dp"
        android:src="@drawable/button_plus"
        android:scaleType="center"
        fab:fab_type="normal"
        fab:fab_colorNormal="@color/FloatingActionButton"
        fab:fab_colorPressed="@color/FloatingActionButtonPressed"
        fab:fab_colorRipple="@color/FloatingActionButtonRipple" />

</FrameLayout>

"change" :

FrameLayout pnlFabContainer = (FrameLayout)view.findViewById(R.id.pnlFabContainer);
LayoutTransition transition = pnlFabContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);

animateFabButton(fab);

- :

protected void animateFabButton(FloatingActionButton fab) {
    if (fab.isSelected()) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)fab.getLayoutParams();
        layoutParams.gravity = Gravity.TOP | Gravity.END;
        layoutParams.topMargin = -(fab.getMeasuredHeight() / 2);
        fab.setLayoutParams(layoutParams);

        Animation rotate = new RotateAnimation(0, 45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setFillBefore(true);
        rotate.setFillAfter(true);
        rotate.setFillEnabled(true);
        rotate.setDuration(750);
        rotate.setRepeatCount(0);
        rotate.setInterpolator(new LinearInterpolator());
        fab.startAnimation(rotate);
    }
    else {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)fab.getLayoutParams();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
        layoutParams.topMargin = layoutParams.bottomMargin;
        fab.setLayoutParams(layoutParams);

        Animation rotate = new RotateAnimation(45, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setFillBefore(true);
        rotate.setFillAfter(true);
        rotate.setFillEnabled(true);
        rotate.setDuration(750);
        rotate.setRepeatCount(0);
        rotate.setInterpolator(new LinearInterpolator());
        fab.startAnimation(rotate);
    }
}
+5

layout_gravity. , .

Animation animation = new TranslateAnimation(0, 500,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
myImage.startAnimation(animation);

+500 x

+1

Source: https://habr.com/ru/post/1523684/


All Articles