0

OK so when i click on my ConstraintLayout my RecyclerView should became VISIBLE and when i click again on the same ConstraintLayout my RecyclerView become GONE. That part works fine...but it's instant or otherwise my animation does not work .I wanted my animation to last a second or so,buth it's instant and looks ugly.

my code for expand:

 public static void expand(final View v) {
    int matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec(((View) v.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
    int wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    v.measure(matchParentMeasureSpec, wrapContentMeasureSpec);
    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
                    ? ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // Expansion speed of 1dp/ms
    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density) * 1000);
    v.startAnimation(a);
}

and for collapse:

  public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();
    v.setVisibility(View.GONE);
    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;
        }
    };

    // Collapse speed of 1dp/ms
    a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density) *1000);
    v.startAnimation(a);
}
gojic
  • 363
  • 7
  • 20
  • Are you setting `android:animateLayoutChanges="true"` on your `ConstraintLayout` ? – javdromero Mar 11 '21 at 19:13
  • @javdromero it is better when i use this but it is still fast...for example if i put animations time 5 sec, do not see any difference – gojic Mar 11 '21 at 19:44
  • Have you tried not setting a dynamic value to setDuration? It's in miliseconds, so like 3000 to test – javdromero Mar 11 '21 at 19:55
  • yes the same thing... – gojic Mar 11 '21 at 19:58
  • 1
    On collapse() you are setting v.setVisibility(View.GONE); before the animation has even started,If you are refering to this [answer](https://stackoverflow.com/a/13381228/15298643), it's not the same as you've pasted – javdromero Mar 11 '21 at 20:01

0 Answers0