我试图从回调中增加for循环,除非动画结束,否则我不希望它们增加。不幸的是,我遇到了一个问题,它建议将变量(J和I)设置为final,然后说我不能递增它们,因为它们是封闭的。'
在调用onAnimationEnd时,如何增加i和j变量?
public static void animateMarkerToICSNew(final Marker marker, AnimationRouteObj animationRouteObj) {
List<CoordsTimePair> mCoordsTimePairList = animationRouteObj.getCoordsTimePairList();
for (int i = 0; i < mCoordsTimePairList.size();) {
CoordsTimePair mCoordsTimePair = mCoordsTimePairList.get(i);
int timeInMS = (mCoordsTimePair.getSeconds() * 1000);
LinkedHashSet<LatLng> latlngHashSet = mCoordsTimePair.getLatlngHashSet();
final List<LatLng> latlngList = new ArrayList<LatLng>(latlngHashSet);
for (int j = 0; j < latlngList.size();) {
final LatLngInterpolator latLngInterpolator = new Linear();
latLngInterpolator.interpolate(1, marker.getPosition(), latlngList.get(j));
TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
return latLngInterpolator.interpolate(fraction, startValue, endValue);
}
};
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, latlngList.get(j));
animator.setDuration(timeInMS);
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animator animation) {
//INCREMENT INSIDE HERE
}
@Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
animator.start();
}
}
}
我收到的错误 不能引用封闭范围中定义的非最终局部变量j (这是在我将变量声明为final之前)
无法分配最终的局部变量j,因为它是在封闭类型中定义的 (这是在我将变量指定为final之后)
答案 0 :(得分:1)
View.post
时,您可以使用方法onAnimationEnd
来实现所需,发布Runnable
,从您的集合中提取条目并启动下一个动画。
答案 1 :(得分:1)
不幸的是,正如您现在设置的那样,这不起作用。正如您的错误告诉您的那样,您无法从内部类访问这些变量,除非它们是最终的。我建议做的是从for循环中获取所有代码并将其移动到单独的方法中。然后,您可以使用List<CoordsTimePair>
对象和要设置动画的对象的索引来调用该方法。例如:
private void animateCoords(final List<CoordsTimePair> mCoordsTimePairList, final int i, final int j) {
CoordsTimePair mCoordsTimePair = mCoordsTimePairList.get(i);
// ... insert the rest of your code from the for loop
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
if (j < latlngList.size() - 1)
animateCoords(mCoordsTimePairList, i, j + 1);
else if (i < mCoordsTimePairList.size() - 1)
animateCoords(mCoordsTimePairList, i + 1, 0);
}
这不会像现在这样完全有效,但你能得到一般的想法吗?这样,您不需要for循环或增加最终变量/访问内部类中的非最终变量。您对此方法的第一次调用是animateCoords(mCoordsTimePairList, 0, 0);