http://liuwangshu.cn/application/view/3-animation.html(刘望舒)

https://blog.51cto.com/4259297/1679852(我自己在51CTO上的博客)

案例一:上下移动

选择改变要移动的View的marginTop或者marginBottom作为基准属性,计算起止位置基准属性的值,再通过属性动画生成渐变值,最后通过setLayoutParams去改变基准属性,从而达到上下平移的效果

 /**
     * 平滑竖直移动View
     * @param view
     * @param duration
     * @param detY
     */
    public static void smoothMoveViewByY(View view,int duration,float detY,OnViewMoveVerticalResultListener onViewMoveVerticalResultListener){
        ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
        float startBottomMargin = layoutParams.bottomMargin;
        float endBottomMargin = startBottomMargin - detY;
        ValueAnimator animator = ValueAnimator.ofFloat(startBottomMargin,endBottomMargin);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                float bottomMargin = (Float) animation.getAnimatedValue();
                ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
                float lastBottomMargin = layoutParams.bottomMargin;
                layoutParams.bottomMargin = (int) bottomMargin;
                //改变位置
                view.setLayoutParams(layoutParams);
                float detYFromStart =  (bottomMargin - startBottomMargin);
                float detYFromLastMove =  (bottomMargin - lastBottomMargin);
                float percent = Math.abs(detYFromStart / detY);

                if(onViewMoveVerticalResultListener != null){
                    onViewMoveVerticalResultListener.onMove(-1 * detYFromStart,-1 * detYFromLastMove,detY,percent);
                }
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(onViewMoveVerticalResultListener != null){
                    onViewMoveVerticalResultListener.onMoveFinish(animation);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        //持续时长
        animator.setDuration(duration);
        animator.start();
    }

案例二:无限旋转

属性动画无限旋转

https://cloud.tencent.com/developer/article/1941172
方式1:

ImageView img = findViewById(R.id.img_src);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(img, "rotation", 0, 359);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.setDuration(2000);
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.start();

方式2:

    RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setFillAfter(true);
        animation.setRepeatCount(Animation.INFINITE);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        img.setAnimation(animation);
        animation.start();
分类: view体系

0 条评论

发表回复

您的电子邮箱地址不会被公开。