gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:33 25 4
gpt4 key购买 nike

如果在 api 调用的成功 block 中,展开动画将不起作用。但是,如果我将它从成功 block 中取出,那么它运行良好。

我必须在成功 block 中调用动画 block 。因为数据来自 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());



}
});
}

从 fragment 中调用 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() 调用时,移除监听器,将第一个标志设置为 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;

}
}

关于android - 动画在 api 响应成功回调中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48825693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com