动画-动画在api响应成功回调中不起作用

如果扩展动画在api调用的成功块中不起作用,则它不起作用.但是,如果我把它从成功块中剔除,那么它就可以正常工作.

我必须在成功块内调用动画块.因为数据来自api.

这是示例.

getWeeklyWaterDate方法:

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());



        }
    });
}

从片段中调用getWeeklyWaterDataMethod.

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();
        }

    });

这是动画代码:

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类

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;
    }
}
最佳答案
调用onCreate()或onCreateView()时,需要添加GlobalLayoutListener.

首先添加两个动画标志.如果两者都正确,则动画将开始.

boolean animateFlag1 = false;
boolean animateFlag2 = false;

其次,在onCreate()中添加全局布局侦听器:

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

        }
    });

您可以看到,调用onGlobalLayout()时,删除侦听器,将first标志设置为true并调用animate方法.

在这些之后,对开始动画的回调执行相同的操作.将您的第二个标志设置为true,

@Override
public void success(Result result) {

    animateFlag2 = true;
    animate();

}

这是动画方法,其中包括您之前编写的onGlobalLayout()方法代码.

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;

    }
}

转载注明原文:动画-动画在api响应成功回调中不起作用 - 代码日志