自定义ViewGroup,并添加高仿雪球app分享弹出动画。

xml文件中:

<com.example.qiaojingfei.menuanimate.MenuLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@mipmap/share_sina"
            />
            ...
</com.example.qiaojingfei.menuanimate.MenuLayout>

java代码中:

  1. onMeasure()中测量ViewGroup上级容器的宽和高与其childView的宽和高
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        mWidth = widthSize;
        mHeight = heightSize;
        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int defaultWidth = DimenUtils.dip2px(mContext, 200);
        int defaultHeight = DimenUtils.dip2px(mContext, 100);
        /**
         * 如果是wrap_content设置为我们计算的值
         * 否则:直接设置为父容器计算的值
         */
        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? mWidth
                : defaultWidth, (heightMode == MeasureSpec.EXACTLY) ? mHeight
                : defaultHeight);

2.onLayout()中定位childView的位置。
* 在for循环中遍历,通过getChildAt(i)获取childView;
* 计算childview坐标,并通过childView.layout(left,top,right,bottom)定位;

3.在按钮点击事件中设置分享弹出动画

  AnimationSet animationSet;
//设置透明动画并添加到AnimationSet动画集合中
  AlphaAnimation alphaAnimation0 = new AlphaAnimation(0, 1);
  alphaAnimation0.setDuration(500);
  animationSet = new AnimationSet(true);
  animationSet.addAnimation(alphaAnimation0);
//设置平移动画并添加到AnimationSet动画集合中        
  fromXDel = padding + (mWidth / 3) - padding;
  TranslateAnimation animation = new TranslateAnimation(fromXDel, 0, 0, 0);
  animation.setDuration(500);
  animationSet.addAnimation(animation);

  animationSet.setFillAfter(true);
  child.startAnimation(animationSet);

这里要注意TranslateAnimation()这个构造方法 :TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
* float fromXDelta 动画开始的点离当前View X坐标上的差值
* float toXDelta 动画结束的点离当前View X坐标上的差值
* float fromYDelta 动画开始的点离当前View Y坐标上的差值
* float toYDelta 动画结束的点离当前View Y坐标上的差值

以上都是与view原始坐标的差值,而不是具体的坐标。

项目地址:https://github.com/Tobi1025/MenuAnimate,有帮助的话希望大家点星给波支持哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值