Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animation not working in api response success callback

The expand animation not working if its in the success block of the api call. But if i put it out from the success block then its working well.

I have to call animation block inside the success block. Because the data is coming from the api.

Here is the sample.

getWeeklyWaterDate method:

public void getWeeklyWaterData(final WaterDataCallBack callBack)
{

    // Find last monday
    Calendar lastMonday = Calendar.getInstance();
    while (lastMonday.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
    {
        lastMonday.add(Calendar.DATE, -1);
    }

    Calendar now = Calendar.getInstance();

    lastMonday.add(Calendar.DAY_OF_YEAR, -1);
    lastMonday.set(Calendar.HOUR, 24);
    lastMonday.set(Calendar.MINUTE, 0);
    lastMonday.set(Calendar.SECOND, 0);
    lastMonday.set(Calendar.MILLISECOND, 0);


    HashMap<String, String> params = new HashMap<>();
    params.put("startDate", Utils.formatYYMMDDDTHHMMSSz(lastMonday.getTimeInMillis()));
    params.put("endDate", Utils.formatYYMMDDDTHHMMSSz(now.getTimeInMillis()));

    ServiceConnector.groupamaAPI.getMyWaters(params).enqueue(new SuccessCallback<MyWaterResponse>() {
        @Override
        public void onSuccess(Response<MyWaterResponse> response) {
            super.onSuccess(response);

            callBack.onWaterDataReceived(response.body());



        }
    });
}

calling getWeeklyWaterDataMethod from the fragment.

WaterHelper.getCurrent().getWeeklyWaterData(new WaterHelper.WaterDataCallBack() {
        @Override
        public void onWaterDataReceived(MyWaterResponse waterResponse) {

            Collections.reverse(waterResponse.waterLogs);
            for (Integer i = 0 ; i < waterResponse.waterLogs.size() ; i++)
            {
                WaterLog waterLog = waterResponse.waterLogs.get(i);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(waterLog.LogDate);
                dates.add(calendar);
                waterAmounts.add(new WaterAmount(calendar, waterLog.WaterMililiter.intValue()));

            }

            prepareTotalAndAverage(waterResponse.waterAverage);
            prepareGraph2();
        }

    });

here is the animation code:

final Integer finalGraphMaxY = 6000;
            chartContainerLL.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
            {
         @Override
                public void onGlobalLayout()
                {
                    chartContainerLL.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    // Animate Bars
                    Integer totalHeight = chartContainerLL.getHeight();

                    ExpandHeightAnimation expandHeightAnimation = new ExpandHeightAnimation(barMonday, 55);
                    expandHeightAnimation.setDuration(800/* animation time */);
                    barMonday.startAnimation(expandHeightAnimation);

                    ExpandHeightAnimation expandHeightAnimation2 = new ExpandHeightAnimation(barTuesday, 30);
                    expandHeightAnimation2.setDuration(800/* animation time */);
                    barTuesday.startAnimation(expandHeightAnimation2);

                    ExpandHeightAnimation expandHeightAnimation3 = new ExpandHeightAnimation(barWednesday, 40);
                    expandHeightAnimation3.setDuration(800/* animation time */);
                    barWednesday.startAnimation(expandHeightAnimation3);


                    ExpandHeightAnimation expandHeightAnimation4 = new ExpandHeightAnimation(barThursday, 30);
                    expandHeightAnimation4.setDuration(800/* animation time */);
                    barThursday.startAnimation(expandHeightAnimation4);


                    ExpandHeightAnimation expandHeightAnimation5 = new ExpandHeightAnimation(barFriday, 40);
                    expandHeightAnimation5.setDuration(800/* animation time */);
                    barFriday.startAnimation(expandHeightAnimation5);

                    if(dates.size() >= 6 && dates.get(5).get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
                    {
                        ExpandHeightAnimation expandHeightAnimation6 = new ExpandHeightAnimation(barSaturday, totalHeight * getNeededDayWaterAmount(Calendar.SATURDAY).mililiter / finalGraphMaxY);
                        expandHeightAnimation6.setDuration(800/* animation time */);
                        barSaturday.startAnimation(expandHeightAnimation6);
                    }


                    if(dates.size() >= 7 && dates.get(6).get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
                    {
                        ExpandHeightAnimation expandHeightAnimation7 = new ExpandHeightAnimation(barSunday, totalHeight * getNeededDayWaterAmount(Calendar.SUNDAY).mililiter / finalGraphMaxY);
                        expandHeightAnimation7.setDuration(800/* animation time */);
                        barSunday.startAnimation(expandHeightAnimation7);
                    }
                }
            });

ExpandHeightAnimation Class

public class ExpandHeightAnimation extends Animation
{
    int targetHeight;
    View view;

    public ExpandHeightAnimation(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}
like image 968
Ali Gürelli Avatar asked Dec 08 '25 21:12

Ali Gürelli


1 Answers

You need to add GlobalLayoutListener when onCreate() or onCreateView() called.

First add two flags for animation. If these both are true, your animation will start.

boolean animateFlag1 = false;
boolean animateFlag2 = false;

Secondly, add your global layout listener inside onCreate():

llContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            llContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            animateFlag1 = true;
            animate();

        }
    });

You can see that when onGlobalLayout() called, remove listener, set first flag to true and call animate method.

After these, do the same thing for your callback which starts to animation. Set your second flag as true,

@Override
public void success(Result result) {

    animateFlag2 = true;
    animate();

}

Here is the animate method, which includes your onGlobalLayout() method codes you wrote before.

private void animate(){
    if(animateFlag1 && animateFlag2){

        // Animate Bars
        ExpandHeightAnimation expandHeightAnimation = new ExpandHeightAnimation(view, 500);
        expandHeightAnimation.setDuration(800/* animation time */);
        view.startAnimation(expandHeightAnimation);

        //More your codes to animate


        //Set your all flags to false to prevent animating again
        animateFlag1 = animateFlag2 = false;

    }
}
like image 191
Oğuzhan Döngül Avatar answered Dec 11 '25 11:12

Oğuzhan Döngül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!