Android实现支付宝蚂蚁森林水滴浮动效果

所属分类: 软件编程 / Android 阅读数: 47
收藏 0 赞 0 分享

可以有多个水滴,可以控制位置,水滴上下浮动。点击水滴产生搜集动画,水滴向树移动并逐渐消失,如图:

那么是如何实现的呢,下面我们一步步来分析:

1、定义一个继承Relativelayout 的子类作为容器放置多个水滴并在Onlayout()中设置子控件的位置

@Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
 
    for (int i = 0; i < count; i++) {
      View child = getChildAt(i);
      int childWidth = child.getMeasuredWidth();
      int childHeight = child.getMeasuredHeight();
      if (child.getVisibility() != GONE) {
        child.layout(listX.get(i), listY.get(i), childWidth + listX.get(i), childHeight + listY.get(i));
      }
    }
  }

上面代码最重要的就是child.layout()函数,前两个参数为子控件的位置,这我先去盗个图:

如图,前两个参数分别为getLeft 和getTop,后两个参数分别为getRight和getBottom;前两个参数其实是我们重外界传进来的子坐标列表,代码如下:

List<Integer> listX = new ArrayList<>();
  List<Integer> listY = new ArrayList<>();
 
  public void setChildPosition(int posx, int posy) {
    listX.add(posx);
    listY.add(posy);
  }

对于后面两个参数我们需要先获取子控件的宽高;然后在叠加上前面两个参数就是我们需要的坐标,在上面代码中可以看到我们是通过child.getmeasure来获取的宽高,但在获取宽高之前我们还需要去测量子空间的宽高。这个测量需要在measure()中完成:

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    measureChildren(widthMeasureSpec, heightMeasureSpec);
  }

至此,我们的父容器已经设计完成,接下来我们需要自己定义子控件以及子控件的动画,首先是一个浮动的动画,因为我们这里后面需要监听点击动作,所以最好使用属性动画完成,如下:

private void doRepeatAnim() {
    ObjectAnimator animator = ObjectAnimator.ofFloat(this, "translationY", -padding, padding, -padding);
    animator.setRepeatMode(ObjectAnimator.REVERSE);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.setDuration(1500);
    animator.start();
  }

就是让其沿Y轴上下移动,设置为INFINTE则为无限重复动画;第二个动画就是我们点击的时候,子控件会移动到某个特定的位置并逐渐消失:

 this.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        doSetAnim();
      }
    });
private void doSetAnim() {
    if (isCollect) return;
    isCollect = true;
    ObjectAnimator move1 = ObjectAnimator.ofFloat(this, "translationX", startWidth, endWidth);
    ObjectAnimator move2 = ObjectAnimator.ofFloat(this, "translationY", startHeight, endHeight);
    ObjectAnimator move3 = ObjectAnimator.ofFloat(this, "alpha", 1, 0);
 
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(move1, move2, move3);
    animatorSet.setDuration(1500);
    animatorSet.start();
  }


里面我添加了isCollect 判断,用于处理点击事件重复生效的问题,这里是一个动画组合,重当前位置移动到特定位置同时透明度也不断的变淡。写动画的时候特别应该注意一个问题就是当前的所有位置都不是外面传进来的位置而是以当前控件初始位置为参考的相对位置,因为我们在父控件的时候就设定好了子控件的位置,不能再次进行重复设定不然会叠加,所以上面的startwidth 和startHeight其实都是0,endWidth 和endHeight也是结束位置减去控件移动的初始位置:

/**
   * @param context
   */
  public WaterView(Context context) {
    super(context);
    this.context = context;
    endWidth = (int) DeviceUtils.dpToPixel(context, 160);
    endHeight = (int) DeviceUtils.dpToPixel(context, 300);
    padding = (int) DeviceUtils.dpToPixel(context, 10);
    startWidth = 0;
    startHeight = 0;
  }
 
  /**
   * @param index
   * @param startWidth 开始坐标 X
   * @param startHeight 开始坐标 Y
   */
  public void setPosition(int index, int startWidth, int startHeight) {
    this.index = index;
    endWidth = endWidth - startWidth;
    endHeight = endHeight - startHeight;
  }

,在设置动画之前,我们还缺少初始化控件的步骤,这个步骤就是绘制背景控件,这个过程在ondraw()方法中进行:

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mMWidth = getMeasuredWidth();
    mHeight = getMeasuredHeight();
  }
 
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(getResources().getColor(R.color.color_87d1ab));
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(mMWidth / 2, (float) mHeight / 2, DeviceUtils.dpToPixel(context, 30), mPaint);
 
    mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(Color.WHITE);
    mTextPaint.setTextSize(DeviceUtils.dpToPixel(context, 30));
    mTextPaint.setColor(getResources().getColor(R.color.text_color_fc));
    mTextPaint.setStyle(Paint.Style.FILL);
    float width = mTextPaint.measureText(text);
    canvas.drawText(text, mMWidth / 2 - width / 2, (float) mHeight * 0.65f, mTextPaint);
 
    doRepeatAnim();
    this.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        doSetAnim();
      }
    });
  }

如上,我绘制了一个纯色的园和特定的文字。当子控件绘制完成后才进行的动画。

最后就是如何使用我们刚才做好了轮子啦,请看代码:

@Override
  protected void onStart() {
    super.onStart();
    int posx = (int) DeviceUtils.dpToPixel(this, 70);
    int posy = (int) DeviceUtils.dpToPixel(this, 70);
    addChildView(this, relative, 1, posx, posy);
    addChildView(this, relative, 2, 2 * posx, 2 * posy);
    addChildView(this, relative, 3, 3 * posx, posy);
  }
 
  /**
   * 添加子水滴
   *
   * @param relative
   * @param index  第几个
   * @param posx   子控件初始位置x
   * @param posy   子控件初始位置y
   */
  private void addChildView(final Context context, final WaterContainer relative, final int index, final int posx, final int posy) {
    relative.postDelayed(new Runnable() {
      @Override
      public void run() {
        int width = (int) DeviceUtils.dpToPixel(context, 60);
        int height = (int) DeviceUtils.dpToPixel(context, 60);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
        WaterView waterView = new WaterView(context);
        waterView.setPosition(index, posx, posy);
        waterView.setLayoutParams(layoutParams);
        relative.setChildPosition(posx, posy);
        relative.addView(waterView);
      }
    }, (index - 1) * 300);
  }

在添加代码里面,我添加了一个延时,这样每个添加的子水滴就会不同步的上下跳动,看起来更为真实,如果你有更好的办法请一定记得告诉我,上面的代码就是通过LayoutParams先设定子控件的布局,再把子控件添加到父容器中去。可以实现重复调用,就是这么简单。

最后给出项目的github地址:链接地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多