Android(安卓)SDK示例代码学习(1)----BouncingBalls
16lz
2021-01-26
最近在学习android的开发,打算把sdk的示例代码研究一下,一直以来都没有做笔记的习惯,以至于看过的东西记住的少遗忘的多,因此试试记下笔记,以备以后查询。
-----------------------------以上是首博呢喃------------------------------------------------------
BouncingBalls主要使用的是ValueAnimator及其子类ObjectAnimator
首先是采用了ValueAnimator及其子类ObjectAnimator来设置背景颜色渐变的动画效果
//创建一个值渐变动画,其中backgroundColor是View中的属性,即View类中有setBackgroundColor方法ValueAnimatorcolorAnim=ObjectAnimator.ofInt(this,"backgroundColor",RED,BLUE);colorAnim.setDuration(3000);colorAnim.setEvaluator(newArgbEvaluator());//设置颜色插值器colorAnim.setRepeatCount(ValueAnimator.INFINITE);colorAnim.setRepeatMode(ValueAnimator.REVERSE);colorAnim.start();
自定义了ShapHolder类保存用于绘制ball的属性
publicclassShapeHolder{privatefloatx=0,y=0;//ball的中心点坐标privateShapeDrawableshape;//ball的图形privateintcolor;privateRadialGradientgradient;//ball的环形渲染器,没有用到,渲染器已经设置到paint里边了privatefloatalpha=1f;privatePaintpaint;.....}
ball的动画效果通过一组ValueAnimator叠加来完成
//ball加速下落,还没有碰到下边框的动画ValueAnimatorbounceAnim=ObjectAnimator.ofFloat(newBall,"y",startY,endY);bounceAnim.setDuration(duration);bounceAnim.setInterpolator(newAccelerateInterpolator());//设置加速//ball碰到下边框后压扁的效果,宽度拉长一倍,所以中心点要相应调整ValueAnimatorsquashAnim1=ObjectAnimator.ofFloat(newBall,"x",newBall.getX(),newBall.getX()-25f);squashAnim1.setDuration(duration/4);squashAnim1.setRepeatCount(1);//压扁和回弹是逆向过程,所以这里需要重复一次squashAnim1.setRepeatMode(ValueAnimator.REVERSE);//设置重复的那次动画是逆向过程squashAnim1.setInterpolator(newDecelerateInterpolator());//设置减速//ball碰到下边框后压扁的效果,宽度拉长一倍ValueAnimatorsquashAnim2=ObjectAnimator.ofFloat(newBall,"width",newBall.getWidth(),newBall.getWidth()+50);squashAnim2.setDuration(duration/4);squashAnim2.setRepeatCount(1);squashAnim2.setRepeatMode(ValueAnimator.REVERSE);squashAnim2.setInterpolator(newDecelerateInterpolator());//ball碰到下边框后压扁的效果,高度变为原来的一半,中心点相应调整ValueAnimatorstretchAnim1=ObjectAnimator.ofFloat(newBall,"y",endY,endY+25f);stretchAnim1.setDuration(duration/4);stretchAnim1.setRepeatCount(1);stretchAnim1.setInterpolator(newDecelerateInterpolator());stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);//ball碰到下边框后压扁的效果,高度变为原来的一半ValueAnimatorstretchAnim2=ObjectAnimator.ofFloat(newBall,"height",newBall.getHeight(),newBall.getHeight()-25);stretchAnim2.setDuration(duration/4);stretchAnim2.setRepeatCount(1);stretchAnim2.setInterpolator(newDecelerateInterpolator());stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);//ball回弹的效果ValueAnimatorbounceBackAnim=ObjectAnimator.ofFloat(newBall,"y",endY,startY);bounceBackAnim.setDuration(duration);bounceBackAnim.setInterpolator(newDecelerateInterpolator());
ball回弹完成之后,有一个逐渐消失的过程动画
ValueAnimatorfadeAnim=ObjectAnimator.ofFloat(newBall,"alpha",1f,0f);fadeAnim.setDuration(250);fadeAnim.addListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationEnd(Animatoranimation){//消失动画完成后删除ballballs.remove(((ObjectAnimator)animation).getTarget());}});
更多相关文章
- 【转】Android实现人人网点击“+”弹出效果
- Android开发之ViewFlipper
- Android---添加结构线或边框的几种方式
- Android(安卓)ApiDemos示例解析(52):Graphics->AnimateDrawables
- Android(安卓)4.4+ 实现半透明状态栏(Translucent Bars)
- android-在代码中实现按下Home键的效果
- Android半透明提示效果的实现
- 2018-08-02 Android中实现阴影和图片圆角效果
- Android的动画简单学习