属性动画+贝塞尔曲线实现落叶效果~~~(@_@;)

之前看了一款有点黄的17app底角的爱心各种乱飞,好奇这种效果的实现方式,恰巧看到这篇文章:程序亦非猿:一步一步教你实现Periscope点赞效果,遂按照其思路实现了一个落叶飘零的效果,如下动图:

看起来还蛮带感的( ˘͈ ᵕ ˘͈ )

实现的要点如下:
  1. 值动画的使用
  2. 贝塞尔公式估值器的设置
  3. 落叶的起点、途径点、终点处理
  4. Activity退出时动画和子线程的处理,防止内存泄露

实现步骤:

① 控件初始化添加叶子集合和补间器集合
    public FloatLeafLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

        // 四张不同形状的叶子
        mLeafs = new Drawable[]{getResources().getDrawable(R.mipmap.leaf_1),
                getResources().getDrawable(R.mipmap.leaf_2),
                getResources().getDrawable(R.mipmap.leaf_3),
                getResources().getDrawable(R.mipmap.leaf_4)};

        // 四个不同的补间器
        mInterpolator = new Interpolator[]{
  new AccelerateDecelerateInterpolator(),
                new AccelerateInterpolator(),
                new DecelerateInterpolator(),
                new LinearInterpolator()};

    }
② onMeasure()测出宽高,并且添加树,树的图片做了缩放处理
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidthSize = measure(widthMeasureSpec), mHeightSize = measure(heightMeasureSpec));
        if (getChildCount() == 0) {
            addTree(mWidthSize, mHeightSize);
        }
    }

    private int measure(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = dip2px(getContext(), 300);
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }

    // 添加树的图片
    private void 
  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值