最近在看github的时候看见了好多好有趣的效果,便想要参考上面的项目,自己自主的实现出来,比如下面这个:
android自定义钟摆loadingView:
可以看到这个实现了自定义钟摆的效果,接下来进行实现的分析。
我们可以从中看到其实就只有三部分内容:
第一部分就是左边第一个小圆球的摇摆。
第二部分就是中间三个小球的震动。
第三部分就是最后一个小球的摇摆。
由于第一部分跟第三部分相当于镜像的感觉,所以实现第一个就相当于实现了第三个,所以只说明第一部分的代码,代码如下;
//旋转动画
leftRotate = new RotateAnimation(0, DEGREE, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, -2f);
//重复的次数
leftRotate.setRepeatCount(1);
//设置旋转的模式,当前为反转模式
leftRotate.setRepeatMode(Animation.REVERSE);
leftRotate.setDuration(DURATION);
//插值器
leftRotate.setInterpolator(new LinearInterpolator());
//监听事件
leftRotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startLefeShake();
startRightRotate();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
主要使用了旋转动画进行实现,了解动画一下子就能看出这是啥了,就不过多叙述了。
我们主要工作是在动画结束的时候开启中间小球的震动以及第三部分的摇摆:
startLefeShake();
startRightRotate();
private void startLefeShake(){
two.startAnimation(leftShake);
three.startAnimation(leftShake);
four.startAnimation(leftShake);
}
private void startRightShake(){
two.startAnimation(rightShake);
three.startAnimation(rightShake);
four.startAnimation(rightShake);
}
接下来看中间部分,我们使用平移动画进行小球的震动操作:
leftShake=new TranslateAnimation(0, 2, 0, 0);
leftShake.setDuration(DURATION);
leftShake.setInterpolator(new CycleInterpolator(2));
rightShake=new TranslateAnimation(0, -2, 0, 0);
rightShake.setDuration(DURATION);
rightShake.setInterpolator(new CycleInterpolator(2));
主要使用了CycleInterpolator这个插值器。
其实熟悉动画的朋友一看就能做出来,但是还是要多练,才能熟悉使用各种api,所以就有了这个项目。
想看全部的朋友可以去的github中下载:
https://github.com/JerryChan123/android-learning