垃圾清理动画

来源:互联网 发布:php字符串复制几个 编辑:程序博客网 时间:2024/05/10 07:29
   小编就职于一家研发手机工具软件的开发公司,这是小编的第一篇博客,我会坚持下去,把自己的心得分享给大家,希望大家也要在自己的道路上坚持下去,在软件开发的道路上越走越好!

 这篇博客给大家带来一个简单的功能实现,在垃圾扫描的同时加入动画,接到的需求是 滑块2秒滑过去,掉头反转,2s滑回来,停顿0.8s在次反转,重新开始循环。好了就拿这个比较简单的动画需求实现来作为我的第一篇博客,动画的效果类似于这种:


 这个动画需求的难点是 循环中的图片的反转,如果采用roation动画反转,不管setDuration(xxx) 时间设置的再短,还是不会达到立马反转的效果,所以我们考虑 ImageView的 一个属性方法 setRoation(180) setRoation(0)这个方法可以使图片立即反转 180度。接下来我们看代码的实现:

private void startCleanAnim() {    ImageView slipperImage = (ImageView) findViewById(R.id.iv_slipper);    slipperImage.setVisibility(View.VISIBLE);    int screenWidth = DeviceUtil.getScreenWidth();    int width = slipperImage.getWidth();    mAnimLeftToRight = ObjectAnimator.ofFloat(slipperImage, "x", -width, screenWidth);    mAnimLeftToRight.setDuration(2000);    mAnimLeftToRight.setInterpolator(new LinearInterpolator());    mAnimRightToLeft = ObjectAnimator.ofFloat(slipperImage, "x", screenWidth, -width);    mAnimRightToLeft.setDuration(2000);    mAnimRightToLeft.setInterpolator(new LinearInterpolator());    mAnimLeftToRight.addListener(new Animator.AnimatorListener() {        @Override        public void onAnimationStart(Animator animation) {        }        @Override        public void onAnimationEnd(Animator animation) {            findViewById(R.id.iv_slipper).setRotation(180);            mAnimRightToLeft.start();        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }    });    mAnimRightToLeft.addListener(new Animator.AnimatorListener() {        @Override        public void onAnimationStart(Animator animation) {        }        @Override        public void onAnimationEnd(Animator animation) {            findViewById(R.id.iv_slipper).setRotation(0);            mAnimLeftToRight.setStartDelay(800);            mAnimLeftToRight.start();        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }    });    mAnimLeftToRight.start();}private void stopCleanAnim(){    if (mAnimRightToLeft != null){        mAnimRightToLeft.cancel();        mAnimRightToLeft.removeAllListeners();    }    if (mAnimLeftToRight != null){        mAnimLeftToRight.cancel();        mAnimLeftToRight.removeAllListeners();    }    findViewById(R.id.iv_slipper).clearAnimation();    findViewById(R.id.iv_slipper).setVisibility(View.INVISIBLE);}}
  iv_slipper滑块资源文件,我会在博客结尾上传给大家, ObjectAnimator.ofFloat()属性动画的实现,依次传入4个参数,简单来讲第一个参数:你需要做动画的控件,第二个参数:你想对控件的什么属性做动画,第三个参数:属性开始的状态(不同的属性会有所不同),第四个参数:属性结束的状态。

int screenWidth = DeviceUtil.getScreenWidth();int width = slipperImage.getWidth();
分析清楚位移的距离 为滑块本身的长度+屏幕的宽度,在这里需要注意的是 在获取控件宽度的时候,布局文件中控件的Visibility属性不能为gone,可以设置为invisible 或者将透明度设置为0,如果设置为gone的时候,控件会规避android的初始化测绘,也就是说布局的时候找不到你的控件,这时候你在获取已经设置为gone控件的宽度的时候就会得到0。这样会影响你动画的实现。

mAnimLeftToRight = ObjectAnimator.ofFloat(slipperImage, "x", -width, screenWidth);mAnimLeftToRight.setDuration(2000);mAnimLeftToRight.setInterpolator(new LinearInterpolator());mAnimRightToLeft = ObjectAnimator.ofFloat(slipperImage, "x", screenWidth, -width);mAnimRightToLeft.setDuration(2000);mAnimRightToLeft.setInterpolator(new LinearInterpolator());
设置位移动画的时间,插值器 LinearInterpolator()动画以常量速率来改变,

mAnimLeftToRight.addListener(new Animator.AnimatorListener() {        @Override        public void onAnimationStart(Animator animation) {        }        @Override        public void onAnimationEnd(Animator animation) {            findViewById(R.id.iv_slipper).setRotation(180);            mAnimRightToLeft.start();        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }    });    mAnimRightToLeft.addListener(new Animator.AnimatorListener() {        @Override        public void onAnimationStart(Animator animation) {        }        @Override        public void onAnimationEnd(Animator animation) {            findViewById(R.id.iv_slipper).setRotation(0);            mAnimLeftToRight.setStartDelay(800);            mAnimLeftToRight.start();        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }    });    mAnimLeftToRight.start();}
分别为两个动画设置监听,开始从左边滑动到右边,在动画结束的时候,图片反转180度 setRotation(180), 开始 右边滑动到左边的动画,mAnimRightToLeft结束以后 图片反转回来 setRotation(0),在开启 mAnimLeftToRight 这样就形成了动画的循环。

private void stopCleanAnim(){    if (mAnimRightToLeft != null){        mAnimRightToLeft.cancel();        mAnimRightToLeft.removeAllListeners();    }    if (mAnimLeftToRight != null){        mAnimLeftToRight.cancel();        mAnimLeftToRight.removeAllListeners();    }    findViewById(R.id.iv_slipper).clearAnimation();    findViewById(R.id.iv_slipper).setVisibility(View.INVISIBLE);}
垃圾清理结束 动画也要随之结束,这里要移除对动画的所有监听,因为你是靠监听来循环动画的。这样整个动画的流程就结束了,

<LinearLayout    android:id="@+id/layout_anim_linear"    android:layout_width="match_parent"    android:layout_height="8dp"    android:orientation="horizontal"    android:layout_alignParentBottom="true"    >    <ImageView        android:id="@+id/iv_slipper"        android:layout_width="42dp"        android:layout_height="8dp"        android:layout_marginLeft="-42dp"        android:visibility="invisible"        android:background="@drawable/huakuai"        /></LinearLayout>
  我的布局文件,垃圾清理的时候 滑块的invisible变为visible 垃圾清理结束,动画结束 滑块的在变为invisible, 另外android:layout_marginLeft = "-42dp" 将滑块布局在屏幕可见宽度的左边,这样每次滑动的时候 会有滑块滑出屏幕的效果,在滑出来的时候,滑块已经反转结束。

 下一篇博客我会给大家分析另外一种比较复杂的垃圾清理动画。

  

2 0
原创粉丝点击