动画无效api响应成功回调

时间:2018-02-16 11:21:40

标签: android animation

如果扩展动画位于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 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;
    }
}

2 个答案:

答案 0 :(得分:3)

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()被调用时,请删除侦听器,将第一个标志设置为 true 并调用animate方法。

在这些之后,对开始动画的回调做同样的事情。将您的第二个标记设置为 true

@Override
public void success(Result result) {

    animateFlag2 = true;
    animate();

}

这是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;

    }
}

答案 1 :(得分:0)

尝试替换此

prepareTotalAndAverage(waterResponse.waterAverage);
prepareGraph2();

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
     prepareTotalAndAverage(waterResponse.waterAverage);
     prepareGraph2();
  }
}, 1);

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        prepareTotalAndAverage(waterResponse.waterAverage);
        prepareGraph2();
    }
});

它有点诡计,但对我有用。