Android实现跳动的小球加载动画效果

先来看看效果图

跳动的小球做这个动画,需掌握:

1、属性动画

2、Path类、Canvas类

3、贝塞尔曲线

4、SurfaceView用法

5、自定义attr属性

6 、架构: 状态模式,控制器

7 、自由落体,抛物线等概念

不多说了,直接上码

1.DancingView.java

public class DancingView extends SurfaceView implements SurfaceHolder.Callback {

  public static final int STATE_DOWN = 1;//向下状态
  public static final int STATE_UP = 2;//向上状态

  public static final int DEFAULT_POINT_RADIUS = 10;
  public static final int DEFAULT_BALL_RADIUS = 13;
  public static final int DEFAULT_LINE_WIDTH = 200;
  public static final int DEFAULT_LINE_HEIGHT = 2;
  public static final int DEFAULT_LINE_COLOR = Color.parseColor("#FF9800");
  public static final int DEFAULT_POINT_COLOR = Color.parseColor("#9C27B0");
  public static final int DEFAULT_BALL_COLOR = Color.parseColor("#FF4081");

  public static final int DEFAULT_DOWN_DURATION = 600;//ms
  public static final int DEFAULT_UP_DURATION = 600;//ms
  public static final int DEFAULT_FREEDOWN_DURATION = 1000;//ms

  public static final int MAX_OFFSET_Y = 50;//水平下降最大偏移距离

  public int PONIT_RADIUS = DEFAULT_POINT_RADIUS;//小球半径
  public int BALL_RADIUS = DEFAULT_BALL_RADIUS;//小球半径

  private Paint mPaint;
  private Path mPath;
  private int mLineColor;
  private int mPonitColor;
  private int mBallColor;
  private int mLineWidth;
  private int mLineHeight;

  private float mDownDistance;
  private float mUpDistance;
  private float freeBallDistance;

  private ValueAnimator mDownController;//下落控制器
  private ValueAnimator mUpController;//上弹控制器
  private ValueAnimator mFreeDownController;//自由落体控制器

  private AnimatorSet animatorSet;
  private int state;

  private boolean ismUpControllerDied = false;
  private boolean isAnimationShowing = false;
  private boolean isBounced = false;
  private boolean isBallFreeUp = false;

  public DancingView(Context context) {
    super(context);
    init(context, null);
  }

  public DancingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
  }

  public DancingView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs) {
    initAttributes(context, attrs);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStrokeWidth(mLineHeight);
    mPaint.setStrokeCap(Paint.Cap.ROUND);

    mPath = new Path();
    getHolder().addCallback(this);

    initController();
  }

  private void initAttributes(Context context, AttributeSet attrs) {
    TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.DancingView);
    mLineColor = typeArray.getColor(R.styleable.DancingView_lineColor, DEFAULT_LINE_COLOR);
    mLineWidth = typeArray.getDimensionPixelOffset(R.styleable.DancingView_lineWidth, DEFAULT_LINE_WIDTH);
    mLineHeight = typeArray.getDimensionPixelOffset(R.styleable.DancingView_lineHeight, DEFAULT_LINE_HEIGHT);
    mPonitColor = typeArray.getColor(R.styleable.DancingView_pointColor, DEFAULT_POINT_COLOR);
    mBallColor = typeArray.getColor(R.styleable.DancingView_ballColor, DEFAULT_BALL_COLOR);
    typeArray.recycle();
  }

  private void initController() {
    mDownController = ValueAnimator.ofFloat(0, 1);
    mDownController.setDuration(DEFAULT_DOWN_DURATION);
    mDownController.setInterpolator(new DecelerateInterpolator());
    mDownController.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        mDownDistance = MAX_OFFSET_Y * (float) animation.getAnimatedValue();
        postInvalidate();
      }
    });
    mDownController.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        state = STATE_DOWN;
      }

      @Override
      public void onAnimationEnd(Animator animation) {
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }
    });

    mUpController = ValueAnimator.ofFloat(0, 1);
    mUpController.setDuration(DEFAULT_UP_DURATION);
    mUpController.setInterpolator(new DancingInterpolator());
    mUpController.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        mUpDistance = MAX_OFFSET_Y * (float) animation.getAnimatedValue();
        if (mUpDistance >= MAX_OFFSET_Y) {
          //进入自由落体状态
          isBounced = true;
          if (!mFreeDownController.isRunning() && !mFreeDownController.isStarted() && !isBallFreeUp) {
            mFreeDownController.start();
          }
        }
        postInvalidate();
      }
    });
    mUpController.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        state = STATE_UP;
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        ismUpControllerDied = true;
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationRepeat(Animator animation) {

      }
    });

    mFreeDownController = ValueAnimator.ofFloat(0, 8f);
    mFreeDownController.setDuration(DEFAULT_FREEDOWN_DURATION);
    mFreeDownController.setInterpolator(new DecelerateInterpolator());
    mFreeDownController.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        //该公式解决上升减速 和 下降加速
        float t = (float) animation.getAnimatedValue();
        freeBallDistance = 40 * t - 5 * t * t;

        if (ismUpControllerDied) {//往上抛,到临界点
          postInvalidate();
        }
      }
    });
    mFreeDownController.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        isBallFreeUp = true;
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        isAnimationShowing = false;
        //循环第二次
        startAnimations();
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }
    });

    animatorSet = new AnimatorSet();
    animatorSet.play(mDownController).before(mUpController);
    animatorSet.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        isAnimationShowing = true;
      }

      @Override
      public void onAnimationEnd(Animator animation) {
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }
    });
  }

  /**
   * 启动动画,外部调用
   */
  public void startAnimations() {
    if (isAnimationShowing) {
      return;
    }
    if (animatorSet.isRunning()) {
      animatorSet.end();
      animatorSet.cancel();
    }
    isBounced = false;
    isBallFreeUp = false;
    ismUpControllerDied = false;

    animatorSet.start();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 一条绳子用左右两部分的二阶贝塞尔曲线组成
    mPaint.setColor(mLineColor);
    mPath.reset();
    //起始点
    mPath.moveTo(getWidth() / 2 - mLineWidth / 2, getHeight() / 2);

    if (state == STATE_DOWN) {//下落
      /**************绘制绳子开始*************/
      //左部分 的贝塞尔
      mPath.quadTo((float) (getWidth() / 2 - mLineWidth / 2 + mLineWidth * 0.375), getHeight() / 2 + mDownDistance,
          getWidth() / 2, getHeight() / 2 + mDownDistance);
      //右部分 的贝塞尔
      mPath.quadTo((float) (getWidth() / 2 + mLineWidth / 2 - mLineWidth * 0.375), getHeight() / 2 + mDownDistance,
          getWidth() / 2 + mLineWidth / 2, getHeight() / 2);

      mPaint.setStyle(Paint.Style.STROKE);
      canvas.drawPath(mPath, mPaint);
      /**************绘制绳子结束*************/

      /**************绘制弹跳小球开始*************/
      mPaint.setStyle(Paint.Style.FILL);
      mPaint.setColor(mBallColor);
      canvas.drawCircle(getWidth() / 2, getHeight() / 2 + mDownDistance - BALL_RADIUS, BALL_RADIUS, mPaint);
      /**************绘制弹跳小球结束*************/
    } else if (state == STATE_UP) { //向上弹
      /**************绘制绳子开始*************/
      //左部分的贝塞尔
      mPath.quadTo((float) (getWidth() / 2 - mLineWidth / 2 + mLineWidth * 0.375), getHeight() / 2 + 50 - mUpDistance,
          getWidth() / 2,
          getHeight() / 2 + (50 - mUpDistance));

      //右部分的贝塞尔
      mPath.quadTo((float) (getWidth() / 2 + mLineWidth / 2 - mLineWidth * 0.375), getHeight() / 2 + 50 - mUpDistance,
          getWidth() / 2 + mLineWidth / 2,
          getHeight() / 2);

      mPaint.setStyle(Paint.Style.STROKE);
      canvas.drawPath(mPath, mPaint);
      /**************绘制绳子结束*************/

      mPaint.setStyle(Paint.Style.FILL);
      mPaint.setColor(mBallColor);

      //弹性小球,自由落体
      if (!isBounced) {
        //上升
        canvas.drawCircle(getWidth() / 2, getHeight() / 2 + (MAX_OFFSET_Y - mUpDistance) - BALL_RADIUS, BALL_RADIUS, mPaint);
      } else {
        //自由落体
        canvas.drawCircle(getWidth() / 2, getHeight() / 2 - freeBallDistance - BALL_RADIUS, BALL_RADIUS, mPaint);
      }
    }
    mPaint.setColor(mPonitColor);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(getWidth() / 2 - mLineWidth / 2, getHeight() / 2, PONIT_RADIUS, mPaint);
    canvas.drawCircle(getWidth() / 2 + mLineWidth / 2, getHeight() / 2, PONIT_RADIUS, mPaint);
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    Canvas canvas = holder.lockCanvas();//锁定整个SurfaceView对象,获取该Surface上的Canvas.
    draw(canvas);
    holder.unlockCanvasAndPost(canvas);//释放画布,提交修改
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  }
}

2.DancingInterpolator.java

 public class DancingInterpolator implements Interpolator {
  @Override
  public float getInterpolation(float input) {
    return (float) (1 - Math.exp(-3 * input) * Math.cos(10 * input));
  }
}

3.自定义属性 styles.xml

<declare-styleable name="DancingView">
    <attr name="lineWidth" format="dimension" />
    <attr name="lineHeight" format="dimension" />
    <attr name="pointColor" format="reference|color" />
    <attr name="lineColor" format="reference|color" />
    <attr name="ballColor" format="reference|color" />
</declare-styleable>

注意:颜色、尺寸、参数可以自己测试,调整。

以上就是本文的全部内容,希望对大家的学习和工作能有所帮助哦。

(0)

相关推荐

  • Android 游戏引擎libgdx 资源加载进度百分比显示案例分析

    因为案例比较简单,所以简单用AndroidApplication -> Game -> Stage 搭建框架 一.主入口,无特殊 复制代码 代码如下: public class App extends AndroidApplication { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化Demo initialize(new Demo()

  • Android实现仿慕课网下拉加载动画

    具体实现方法就不多介绍了先附上源码,相信大家都容易看的懂: 这里为了让这个动画效果可被复用,于是就继承了ImageView 去实现某些方法 package com.example.loading_drawable; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.util.AttributeSet; import android.util.Log;

  • Android自定义加载圈动画效果

    本文实例为大家分享了Android自定义加载圈动画展示的具体代码,供大家参考,具体内容如下 实现如下效果: 该效果图主要有3个动画: 1.旋转动画 2.聚合动画 3.扩散动画 以上3个动画都是通过ValueAnimator来实现,配合自定义View的onDraw()方法实现不断的刷新和绘制界面. 具体代码如下: package blog.csdn.net.mchenys.myanimationloading; import android.animation.Animator; import a

  • Android使用glide加载gif动画设置播放次数

    在使用glide加载gif动画,有时需要设置播放的次数,然后播放玩一次或者几次之后,需要在播放完做一些其他的操作,直接看代码: Glide.with(this) .load(R.drawable.xiaoguo) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .listener(new RequestListener<Integer, GlideDrawable>() { @Override public boolean onException(Ex

  • Android自定义加载控件实现数据加载动画

    本文实例为大家分享了Android自定义加载控件,第一次小人跑动的加载效果眼前一亮,相比传统的PrograssBar高大上不止一点,于是走起,自定义了控件LoadingView去实现动态效果,可直接在xml中使用,具体实现如下 package com.*****.*****.widget; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.util.

  • Android自定义view实现阻尼效果的加载动画

    效果: 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减的振动,又称减幅振动.衰减振动.[1] 不论是弹簧振子还是单摆由于外界的摩擦和介质阻力总是存在,在振动过程中要不断克服外界阻力做功,消耗能量,振幅就会逐渐减小,经过一段时间,振动就会完全停下来.这种振幅随时间减小的振动称为阻尼振动.因为振幅与振动的能量有关,阻尼振动也就是能量不断减少的振动.阻尼振动是非简谐运

  • Android自定义加载loading view动画组件

    在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress )的代码 package com.hysmarthotel.view; import com.hysmarthotel.roomcontrol.R; import com.hysmarthotel.util.EaseInOutCubicInterpolator; import android.ani

  • Android加载Gif动画实现代码

    Android加载Gif动画如何实现?相信大家都很好奇,本文就为大家揭晓,内容如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_he

  • Android Glide图片加载(加载监听、加载动画)

    本文实例为大家分享了Android Glide图片加载的具体代码,供大家参考,具体内容如下 1.普通用法 Glide.with(context) .load(url) .into(view); with中可以放context.activity.fragment..:当放activity.fragment时glide会根据生命周期来加载图片.推荐使用activity. 2.设置加载中和加载失败的图片 Glide.with(context) .load(url) .placeholder(R.dra

  • Android绘制圆形百分比加载圈效果

    先看一组加载效果图,有点粉粉的加载圈: 自定义这样的圆形加载圈还是比较简单的,主要是用到Canvans的绘制文本,绘制圆和绘制圆弧的api: /** * 绘制圆 * @param cx 圆心x坐标 * @param cy 圆心y坐标 * @param radius 圆的半径 * @param paint 画笔 */ public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) { ... } /**

随机推荐