Android 如何使用onAnimationend()逐个启动动画?

Android 如何使用onAnimationend()逐个启动动画?,android,animation,view,progress-bar,interpolation,Android,Animation,View,Progress Bar,Interpolation,我正在制作一个由七个圆圈组成的动画 我用插值器在画布上画圆圈,动画很好。但是我希望每个圆都是一个接一个的开始/ 我为此开发了一个库项目,下面是我的java文件 import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; imp

我正在制作一个由七个圆圈组成的动画

我用插值器在画布上画圆圈,动画很好。但是我希望每个圆都是一个接一个的开始/

我为此开发了一个库项目,下面是我的java文件

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.util.Log;
import android.view.animation.LinearInterpolator;

import teamdapsr.loaders.lib.utils.MeasureUtils;

/**
 * Created by Devesh on 08-Jul-15.
 */
public class CubicBezierRotate extends View{

    private int i=0;
    private int radius = 0, centerx, centery;
    private ShapeDrawable circleOne, circleTwo, circleThree, circlesmall;
    protected Paint paint[];
    protected ValueAnimator anim1, anim2, anim3;
    public int getRadius(){ return radius; }
    public void setRadius(int radius){ this.radius = radius; }

    public CubicBezierRotate(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.CubicBezierRotate,
                0, 0
        );

        try
        {
            radius = a.getInt(R.styleable.CubicBezierRotate_radius, 0);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            a.recycle();
        }
        init();
    }

    protected void init()
    {
        circleOne = new ShapeDrawable(new OvalShape());
        circleTwo = new ShapeDrawable(new OvalShape());
        circleThree = new ShapeDrawable(new OvalShape());


        circleOne.getPaint().setColor(0x99ff0000);
        circleTwo.getPaint().setColor(0x9900ff00);
        circleThree.getPaint().setColor(0x990000ff);

        ObjectAnimator animator = ObjectAnimator.ofInt(this, "radius", 0, 100);
        animator.setDuration(5000);
        animator.setInterpolator(new AnticipateOvershootInterpolator());

/*      Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(true) {
                        sleep(50);
                        updateBounds();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };*/


        paint = new Paint[3];

        paint[0] = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint[1] = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint[2] = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint[0].setStyle(Paint.Style.FILL);
        paint[1].setStyle(Paint.Style.FILL);
        paint[2].setStyle(Paint.Style.FILL);

        paint[0].setColor(0x99999999);
        paint[1].setColor(0x99999999);
        paint[2].setColor(0x99999999);

    }

    protected void setupAnimations()
    {

        anim1 = ValueAnimator.ofInt(0, (int)(0.10*getMeasuredHeight()));
        anim2 = ValueAnimator.ofInt(0, (int)(0.10*getMeasuredHeight()));
        anim3 = ValueAnimator.ofInt(0, (int)(0.10*getMeasuredHeight()));

        anim1.setDuration(1500);
        anim2.setDuration(1500);
        anim3.setDuration(1500);

        anim1.setInterpolator(new AccelerateDecelerateInterpolator());
        anim2.setInterpolator(new LinearInterpolator());
        anim3.setInterpolator(new AnticipateOvershootInterpolator());

        anim1.setRepeatMode(ValueAnimator.REVERSE);
        anim2.setRepeatMode(ValueAnimator.REVERSE);
        anim3.setRepeatMode(ValueAnimator.REVERSE);

        anim1.setRepeatCount(ValueAnimator.INFINITE);
        anim2.setRepeatCount(ValueAnimator.INFINITE);
        anim3.setRepeatCount(ValueAnimator.INFINITE);

//      anim2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//          @Override
//          public void onAnimationUpdate(ValueAnimator animation) {
//              int animProgress = (Integer) animation.getAnimatedValue();
//          }
//      });
//
//      anim1.addListener(new AnimatorListenerAdapter() {
//          @Override
//          public void onAnimationEnd(Animator animation) {
//              super.onAnimationEnd(animation);
//          }
//      });
        anim1.start();
        anim2.start();
        anim3.start();
    }
    protected void updateBounds(int radius)
    {
/*
        circleOne.setBounds(radius, radius, radius, radius);
        circleTwo.setBounds(radius, radius, radius, radius);
        circleThree.setBounds(radius, radius, radius, radius);
*/

        invalidate();
        //requestLayout();
    }

    @Override
    public void onDraw(Canvas canvas)
    {

            Log.i("radius", "Radius1 = " + (int) (anim1.getAnimatedValue()));
            Log.i("radius", "Radius2 = " + (int) (anim2.getAnimatedValue()));
            Log.i("radius", "Radius3 = " + (int) (anim3.getAnimatedValue()));

        canvas.drawCircle(50, getMeasuredHeight()/2, (int)(anim1.getAnimatedValue()), paint[0]);

        canvas.drawCircle(90, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[1]);

        canvas.drawCircle(130, getMeasuredHeight()/2, (int)(anim3.getAnimatedValue()), paint[2]);
        canvas.drawCircle(170, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
            canvas.drawCircle(210, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
            canvas.drawCircle(250, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
            canvas.drawCircle(290, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);


//      while (i<7)
//      {
//          switch (i)
//          {
//              case 0 :
//                  canvas.drawCircle(50, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[0]);
//                  canvas.drawCircle(90, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[1]);
//                  canvas.drawCircle(130, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
//                  canvas.drawCircle(170, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
//                  try {
//                      Thread.sleep(1000);
//                  } catch (InterruptedException e) {
//                      e.printStackTrace();
//                  }
//                  i++;
//                  break;
//              case 1 :
//
//                  canvas.drawCircle(210, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
//                  canvas.drawCircle(250, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
//                  canvas.drawCircle(290, getMeasuredHeight()/2, (int)(anim2.getAnimatedValue()), paint[2]);
//                  try {
//                      Thread.sleep(50);
//                  } catch (InterruptedException e) {
//                      e.printStackTrace();
//                  }
//                  break;
//
//          }
//      }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        updateBounds(0);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // Get the width measurement
        int widthSize = MeasureUtils.getMeasurement(widthMeasureSpec, getDesiredWidth());

        // Get the height measurement
        int heightSize = MeasureUtils.getMeasurement(heightMeasureSpec, getDesiredHeight());

        centerx = (int)(0.5*MeasureSpec.getSize(widthMeasureSpec));
        centery = (int)(0.5*MeasureSpec.getSize(heightMeasureSpec));

        //MUST call this to store the measurements
        setMeasuredDimension(widthSize + 10, heightSize + 10);

        setupAnimations();
    }

    private int getDesiredWidth()
    {
        // TO-DO Calculate width from child components.

        return 2*radius+1000;
    }

    private int getDesiredHeight()
    {
        // TO-DO Calculate height from chile components.
        return 2*radius+100;
    }

}
导入android.animation.Animator;
导入android.animation.AnimatorListenerAdapter;
导入android.animation.ObjectAnimator;
导入android.animation.ValueAnimator;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.graphics.Canvas;
导入android.graphics.Paint;
导入android.graphics.drawable.ShapeDrawable;
导入android.graphics.drawable.shapes.OvalShape;
导入android.util.AttributeSet;
导入android.view.view;
导入android.view.animation.AcceleratedCelerateInterpolator;
导入android.view.animation.PreventeOvershootInterpolator;
导入android.util.Log;
导入android.view.animation.LinearInterpolator;
导入teamdapsr.loaders.lib.utils.MeasureUtils;
/**
*由Devesh于2015年7月8日创建。
*/
公共类CubicBezierRotate扩展视图{
私有整数i=0;
专用整数半径=0,centerx,centery;
私人形状可移动圆环,圆环2,圆环3,圆环小;
受保护油漆[];
保护值Animator anim1、anim2、anim3;
public int getRadius(){return radius;}
public void setRadius(int-radius){this.radius=radius;}
公共CubicBezierRotate(上下文、属性集属性)
{
超级(上下文,attrs);
TypedArray a=context.getTheme().ActainStyledAttributes(
属性,
R.styleable.CubicBezierRotate,
0, 0
);
尝试
{
半径=a.getInt(R.styleable.CubicBezierRotate_半径,0);
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
a、 回收();
}
init();
}
受保护的void init()
{
circleOne=新的可变形(新的椭圆形());
circleTwo=新的可变形(新的椭圆形());
circleThree=新的可变形(新的椭圆形());
circleOne.getPaint().setColor(0x99ff0000);
circleTwo.getPaint().setColor(0x9900ff00);
circleThree.getPaint().setColor(0x99000FF);
ObjectAnimator animator=ObjectAnimator.ofInt(这个“半径”,0,100);
动画师。设定持续时间(5000);
animator.setInterpolator(新的PreceedOvershootInterpolator());
/*线程线程=新线程(){
@凌驾
公开募捐{
试一试{
while(true){
睡眠(50);
updateBounds();
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
};*/
油漆=新油漆[3];
油漆[0]=新油漆(油漆.防油漆别名标志);
油漆[1]=新油漆(油漆.防油漆别名标志);
油漆[2]=新油漆(油漆.防油漆别名标志);
paint[0].setStyle(paint.Style.FILL);
paint[1].setStyle(paint.Style.FILL);
paint[2].setStyle(paint.Style.FILL);
绘制[0]。设置颜色(0x9999999);
油漆[1]。设置颜色(0x9999999);
油漆[2]。设置颜色(0x9999999);
}
受保护的void setupAnimations()
{
anim1=ValueAnimator.ofInt(0,(int)(0.10*getMeasuredHeight());
anim2=ValueAnimator.ofInt(0,(int)(0.10*getMeasuredHeight());
anim3=ValueAnimator.ofInt(0,(int)(0.10*getMeasuredHeight());
动画1.设定持续时间(1500);
2.设定持续时间(1500);
3.设定持续时间(1500);
anim1.setInterpolator(新的加速加速插补器());
anim2.setInterpolator(新的LinearInterpolator());
anim3.setInterpolator(新的PreceedOvershootInterpolator());
anim1.setRepeatMode(值animator.REVERSE);
anim2.setRepeatMode(值animator.REVERSE);
anim3.setRepeatMode(值animator.REVERSE);
anim1.setRepeatCount(ValueAnimator.INFINITE);
anim2.setRepeatCount(ValueAnimator.INFINITE);
anim3.setRepeatCount(ValueAnimator.INFINITE);
//anim2.addUpdateListener(新值animator.AnimatorUpdateListener(){
//@覆盖
//动画更新上的公共无效(ValueAnimator动画){
//int animProgress=(整数)animation.getAnimatedValue();
//          }
//      });
//
//anim1.addListener(新的AnimatorListenerAdapter(){
//@覆盖
//AnimationEnd上的公共无效(Animator动画){
//super.onAnimationEnd(动画);
//          }
//      });
anim1.start();
anim2.start();
anim3.start();
}
受保护的void updateBounds(整数半径)
{
/*
圆.立根(半径,半径,半径,半径);
圆两个立根(半径、半径、半径、半径);
圆圈三个立根(半径,半径,半径,半径);
*/
使无效();
//requestLayout();
}
@凌驾
公共空白onDraw(画布)
{
Log.i(“半径”、“半径1=”+(int)(anim1.getAnimatedValue());
Log.i(“半径”、“半径2=”+(int)(anim2.getAnimatedValue());
Log.i(“半径”、“半径3=”+(int)(anim3.getAnimatedValue());
drawCircle(50,getMeasuredHeight()/2,(int)(anim1.getAnimatedValue()),paint[0]);
drawCircle(90,getMeasuredHeight()/2,(int)(anim2.getAnimatedValue()),paint[1]);
drawCircle(130,getMeasuredHeight()/2,(int)(anim3.getAnimatedValue()),paint[2]);
drawCircle(170,getMeasuredHeight()/2,(int)(anim2.getAnimatedValue()),paint[2]);
drawCircle(210,getMeasuredHeight()/2,(int)(anim2.getAnimatedValue()),paint[2]);
canvas.drawCircle(250,getMeasuredHeight()/2,(int)(anim2.ge
<animation-list   android:id="@+id/progressCircles" android:oneshot="false" 
         xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/progress_circle1" android:duration="150" />
        <item android:drawable="@drawable/progress_circle2" android:duration="150" />
        <item android:drawable="@drawable/progress_circle3" android:duration="150" />
        <item android:drawable="@drawable/progress_circle4" android:duration="150" />
        <item android:drawable="@drawable/progress_circle5" android:duration="150" />
        <item android:drawable="@drawable/progress_circle6" android:duration="150" />
        <item android:drawable="@drawable/progress_circle7" android:duration="150" />
     </animation-list> 
<ImageView
  android:id="@+id/progress_bar"
  android:layout_alignParentRight="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/progressCircles" />
ImageView imgProgress = (ImageView)findViewById(R.id.progress_bar);
    if (imgProgress != null) {
        imgProgress.setVisibility(View.VISIBLE);
        AnimationDrawable animation = (AnimationDrawable)progress.getDrawable();
        animation.setCallback(progress);
        animation.setVisible(true, true);
    }