如何在Android中重复查看动画变量的次数?

时间:2019-08-29 16:53:12

标签: java android animation

我有这个swapAnimation方法,它基本上会交换两个视图。我在循环内调用此方法,并每次在swapAnimation方法上传递不同的视图。但是问题是animation仅发生一次。我希望它重复n次。

void swapAnimation(View v1,View v2){

        if(isAnimating)return;
        isAnimating = true;

        float x1,y1,x2,y2;


        x1 =getRelativeX(v1);
        x2 = getRelativeX(v2);

        y1 = getRelativeY(v1);
        y2 = getRelativeY(v2);

        float x_displacement = (x2-x1);
        float y_displacement = (y2-y1);

        v1.animate().xBy(x_displacement).yBy(y_displacement);
        v2.animate().xBy(-x_displacement).yBy(-y_displacement);
        v1.animate().setDuration(500);
        v2.animate().setDuration(500);
        long duration = v1.animate().getDuration();

        new CountDownTimer(duration+10,duration+10){

            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                isAnimating = false;
            }
        }.start();



    }


public static void arrange(LinearLayout container, Context context){

        MainActivity activity = (MainActivity) context;

        for(int i=0;i<container.getChildCount();i++){
            BarView v1 = (BarView) container.getChildAt(i);
            for(int j=i;j<container.getChildCount();j++){

                BarView v2 = (BarView) container.getChildAt(j);

                if(v1.getWeight() > v2.getWeight()){
                    Log.d(TAG, "bubbleSort: "+v1.getWeight()+">"+v2.getWeight());
                    activity.swapAnimation(v1,v2);
                }
            }

        }


    }

1 个答案:

答案 0 :(得分:1)

尝试一下

animation.setRepeatCount(Animation.INFINITE);