applyTransformation 两次调用 interpolatedTime == 1

Mat*_*kov 5 animation android

我有一个ListView项目需要用动画折叠然后删除。我Animation用于折叠项目,并在完成折叠后从 中删除该项目ListView(通过从数据列表中删除它并调用notifyDataSetChanged)。为了检测动画是否完成,我interpolatedTimeapplyTransformation方法中检查是否== 1.0 。问题是applyTransformation使用 `interpolatedTime' == 1 调用了两次,所以我不能真正依赖它(否则我可以删除两个项目而不是一个项目)。为什么会这样?这是我的一些代码:

public static void collapseAndDelete(final View v, final ArrayList<AlarmClock> alarmClockArrayList,
                                     final AlarmsArrayAdapter adapter, final int position) {
    final int initialHeight = v.getMeasuredHeight();
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            if (interpolatedTime == 1) {
                alarmClockArrayList.remove(position);
                adapter.notifyDataSetChanged();
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
                v.setAlpha(1.0f - interpolatedTime);
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };
    a.setDuration(400);
    v.startAnimation(a);
}
Run Code Online (Sandbox Code Playgroud)

Vya*_*lav 1

实现动画侦听器以捕获最终回调

r.setAnimationListener(new OAnimationListener(this));
Run Code Online (Sandbox Code Playgroud)

类的例子:

public class OAnimationListener implements AnimationListener{

    private MyView vv;


    public OAnimationListener(MyView vv) {
        // TODO Auto-generated constructor stub
        this.vv = vv;
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (vv != null)
            vv.stopAnim(2); //or any wanted callback

    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记设置这个:

r.setRepeatCount(0);

                r.setFillAfter(true);
Run Code Online (Sandbox Code Playgroud)