Android 实现卡片堆叠钱包管理动画效果

作者:Sand 时间:2021-12-12 11:15:02 

先上效果图

Android 实现卡片堆叠钱包管理动画效果

源码 github.com/woshiwzy/Ca…

实现原理:

1.继承LinearLayout
2.重写onLayout,onMeasure 方法
3.利用ValueAnimator 实施动画
4.在动画回调中requestLayout 实现动画效果

思路:

1.用Bounds 对象记录每一个CardView 对象的初始位置,当前位置,运动目标位置

2.点击时计算出对应的view以及可能会产生关联运动的View的运动的目标位置,从当前位置运动到目标位置,然后以这2个位置作为动画参数实施ValueAnimator动画,在动画回调中触发onLayout,达到动画的效果。

重写adView 方法

确保新添加的在这里确保所有的子view 都有一个初始化的bounds位置

@Override
   public void addView(View child, ViewGroup.LayoutParams params) {
       super.addView(child, params);
       Bounds bounds = getBunds(getChildCount());
   }

确保每个子View的测量属性宽度填满父组件

boolean mesured = false;
   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       if (mesured == true) {//只需要测量一次
           return;
       }
       mesured = true;
       int childCount = getChildCount();
       int rootWidth = getWidth();
       int rootHeight = getHeight();
       if (childCount > 0) {
           View child0 = getChildAt(0);
           int modeWidth = MeasureSpec.getMode(child0.getMeasuredWidth());
           int sizeWidth = MeasureSpec.getSize(child0.getMeasuredWidth());

int modeHeight = MeasureSpec.getMode(child0.getMeasuredHeight());
           int sizeHeight = MeasureSpec.getSize(child0.getMeasuredHeight());

if (childCount > 0) {
               for (int i = 0; i < childCount; i++) {
                   View childView = getChildAt(i);
                   childView.measure(MeasureSpec.makeMeasureSpec(sizeWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(sizeHeight, MeasureSpec.EXACTLY));
                   int top = (int) (i * (sizeHeight * carEvPercnet));
                   getBunds(i).setTop(top);
                   getBunds(i).setCurrentTop(top);
                   getBunds(i).setLastCurrentTop(top);
                   getBunds(i).setHeight(sizeHeight);
               }

}

}
   }

重写onLayout 方法是关键

是动画触发的主要目的,这里layout参数并不是写死的,而是计算出来的(通过ValueAnimator 计算出来的)

@Override
   protected void onLayout(boolean changed, int sl, int st, int sr, int sb) {
       int childCount = getChildCount();
       if (childCount > 0) {
           for (int i = 0; i < childCount; i++) {
               View view = getChildAt(i);
               int mWidth = view.getMeasuredWidth();
               int mw = MeasureSpec.getSize(mWidth);
               int l = 0, r = l + mw;
               view.layout(l, getBunds(i).getCurrentTop(), r, getBunds(i).getCurrentTop() + getBunds(i).getHeight());
           }
       }
   }

源码

github: github.com/woshiwzy/Ca&hellip;

来源:https://juejin.cn/post/7073371960150851615

标签:Android,卡片堆叠,钱包管理
0
投稿

猜你喜欢

  • 简单实现Java版学生管理系统

    2022-06-22 15:16:19
  • Springboot+ElementUi实现评论、回复、点赞功能

    2022-06-16 04:34:00
  • android使用handlerthread创建线程示例

    2023-07-05 17:29:57
  • C#使用三层架构开发Winform的详细案例

    2023-08-08 03:22:24
  • android计算器代码示例分享

    2023-10-14 14:06:58
  • java实现文件的断点续传

    2023-11-23 09:11:00
  • JavaMail实现带附件的邮件发送

    2021-10-21 15:00:09
  • SpringBoot搭建go-cqhttp机器人的方法实现

    2022-11-17 23:58:40
  • Android 创建与解析XML(四)——详解Pull方式

    2023-06-03 07:10:23
  • java + dom4j.jar提取xml文档内容

    2023-11-29 03:55:10
  • springboot Interceptor拦截器excludePathPatterns忽略失效

    2023-08-11 19:44:46
  • C#实现自定义定时组件的方法

    2023-06-04 12:36:26
  • 详解Flutter网络图片本地缓存的实现

    2023-08-18 19:44:43
  • MyBatis查询数据返回null的解决

    2021-11-17 20:46:48
  • 分享15款Java程序员必备的开发工具

    2021-12-07 19:09:07
  • java组件commons-fileupload实现文件上传、下载、在线打开

    2022-02-24 22:16:59
  • JNI方法实现图片压缩(压缩率极高)

    2021-08-07 11:32:55
  • 浅谈Mybatis传参类型如何确定

    2023-11-12 12:13:21
  • Android图像切换器imageSwitcher的实例应用

    2023-10-06 00:30:56
  • Android仿一点资讯收藏Toast动画效果

    2022-01-15 18:42:33
  • asp之家 软件编程 m.aspxhome.com