如果扩展动画在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,
@OverrIDepublic 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响应成功回调中不起作用全部内容,希望文章能够帮你解决动画-动画在api响应成功回调中不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)