Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate view off Screen in android Constraint Layout

I have a TextView inside a Constraint Layout. I am trying to animate in a way that the view goes off the screen from top. This is what I did so far,

ConstraintSet constraintSet = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout)holder.mView;
constraintSet.clone(layout);

constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.TOP);
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.TOP);  
constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.BOTTOM); 
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.BOTTOM);

constraintSet.setMargin(R.id.txt_PackageTitle,ConstraintSet.TOP,-600); 
constraintSet.setMargin(R.id.txt_PackageDescription,ConstraintSet.TOP,-1200);

ChangeBounds transition = new ChangeBounds();
transition.setInterpolator(new BounceInterpolator());
transition.setDuration(600);                
TransitionManager.beginDelayedTransition(layout,transition);
                    constraintSet.applyTo(layout);

Now this code only moves the contents to the very top of the view, its not going out of the view and getting disappeared.

How can I do this with constraint layout?

like image 309
Easy Coder Avatar asked Nov 02 '17 16:11

Easy Coder


People also ask

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.

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

How to show /hide linear layout with animation in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken button to show /hide linear layout with animation.

Why doesn't constraintlayout have animations for each view?

If not, providing animations for each of the views will be a tiresome job. ConstraintLayout will only perform animation on its direct children since it only knows when you change layout parameters and constraints on the children that it handles. It may not handle ViewGroups well.

How to show and hide a view with slide up/down animation in Android?

This example demonstrate about how to Show and hide a View with a slide up/down animation in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to use constraintlayout features in Android Studio?

Now to use ConstraintLayout features in our android project, we will need to add the library to our build.gradle app module dependencies section. In Android Studio design and blueprint mode are added which display the layout design in design and blueprint mode. You can enable any mode or both together according to your requirement.


1 Answers

Instead of clearing the bottom constraint of the TextView, try constraining its bottom to the top of the ConstraintLayout like this:

constraintSet.connect (R.id.txt_PackageTitle, 
            ConstraintSet.BOTTOM, 
            PARENT_ID, 
            ConstraintSet.TOP);

Now when the view is animated, it should slide off the top edge.

Negative margins are not supported with ConstraintLayout as is noted here.

like image 146
Cheticamp Avatar answered Sep 17 '22 22:09

Cheticamp