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

Posted

技术标签:

【中文标题】动画在 api 响应成功回调中不起作用【英文标题】:animation not working in api response success callback 【发布时间】:2018-02-16 11:21:39 【问题描述】:

如果在 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;
    

【问题讨论】:

【参考方案1】:

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 flag为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;

    

【讨论】:

【参考方案2】:

尝试替换它

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

这有点花招,但对我有用。

【讨论】:

我已经尝试过了,但不幸的是它不起作用:( @AliGürelli ,检查编辑的答案,另一种方式 那么到底发生了什么?我的意思是根本没有动画? 是的。动画没有开始。但我认为它与线程有关。也许我从动画类扩展的动画正试图在成功块的同一线程上工作?但我不知道我该如何解决它

以上是关于动画在 api 响应成功回调中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

matplotlib 中的动画在 spyder 中不起作用

Ajax成功功能在jquery mobile中不起作用

滑动删除 UITableView 单元格动画在 swift 2 中不起作用

jQuery 地址在回调中不起作用

为啥进入动画在新线程中不起作用?

颤振补间基本动画在“FutureBuilder”中不起作用