Skip to content

Commit 03dffce

Browse files
committed
炫酷的进度条
1 parent bb13e5a commit 03dffce

6 files changed

Lines changed: 452 additions & 219 deletions

File tree

README.md

Lines changed: 5 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,10 @@
1-
### 今天开始记录工作中遇到的需要实现的动画效果实现自定义view动画,后期会有一些列动画设计思路的文章。
2-
3-
> 在这里分享的是设计实现思路,仅供学习使用,让大家拿到稍微复杂点的动画的时候要知道该如何去一步步分解实现,而不是抱怨
4-
下边就先来看看设计需要的效果图及我们最终实现的效果图,毕竟有图有真相嘛!
5-
61

2+
# [**炫酷的提交按钮**](https://github.com/lygttpod/AndroidCustomView/blob/master/animation_button.md)
73
![99.gif](http://upload-images.jianshu.io/upload_images/2057501-d710bca1e166fbc4.gif?imageMogr2/auto-orient/strip)
84

95

10-
##### 其实我刚拿到设计图的时候心想,MD直接给一张gif图不就行了何必这个麻烦呐,随后冷静下来之后(其实就是抱怨之后)想想作为一名Android开发者总不能什么动画都依赖设计师吧,那样的话会显得我们开发者没什么卵用啊,说不定还会被设计师鄙视哦,于是就开始了动画分析及实现之旅。
11-
******
12-
### 通过这个gif动画我们分析出动画过程的实质:
13-
> 一个长方形(或者是圆角长方形)逐渐过渡成为两边是半圆的长方形,于此同时长方形两边向中间靠拢最终形成一个圆,然后圆上升一定高度,最后在圆里边画出对勾(✔).整个动画分解的其实就是这几个部分,那么我们该如何实现呐,不要捉急,继续往下看。
14-
15-
## 第一步:我们要先画出一个圆角矩形吧
16-
```
17-
/**
18-
* 绘制带圆角的矩形
19-
*
20-
* @param canvas 画布
21-
*/
22-
private void draw_oval_to_circle(Canvas canvas) {
23-
24-
//这里是对矩形的位置大小的设置
25-
rectf.left = two_circle_distance;
26-
rectf.top = 0;
27-
rectf.right = width - two_circle_distance;
28-
rectf.bottom = height;
29-
30-
//画圆角矩形
31-
canvas.drawRoundRect(rectf, circleAngle, circleAngle, paint);
32-
33-
}
34-
35-
```
36-
37-
##### 圆角矩形绘制完成之后就是改变圆角半径的大小使其两边形成半圆的效果,那么怎么才能让他成为半圆呐,来看看一张图,若要绘制成半圆效果,那么这个圆的直径就是view自身的高度,那么这个圆的半径就是height/2
38-
39-
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2057501-741dbdfc3aa6c568.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
40-
41-
42-
```
43-
44-
/**
45-
* 设置矩形过度圆角矩形的动画
46-
*/
47-
private void set_rect_to_angle_animation() {
48-
animator_rect_to_angle = ValueAnimator.ofInt(0, height / 2);
49-
animator_rect_to_angle.setDuration(duration);
50-
animator_rect_to_angle.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
51-
@Override
52-
public void onAnimationUpdate(ValueAnimator animation) {
53-
circleAngle = (int) animation.getAnimatedValue();
54-
invalidate();
55-
}
56-
});
57-
}
58-
59-
```
60-
添加动画之后的效果如下
61-
62-
![button_1.gif](http://upload-images.jianshu.io/upload_images/2057501-e863b04adb9f2440.gif?imageMogr2/auto-orient/strip)
63-
64-
65-
## 第二步:当矩形两边都是半圆之后就要处理使其向中间靠拢逐渐形成一个圆,那么问题又来了,需要向中间移动多少呐,并且怎么移动才能使两边都想中间聚拢呐
66-
##### 下边来看一张图分析一下
67-
68-
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2057501-4f0ded1fc136ae56.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
69-
70-
##### 有图可知移动的距离是(width-height)/2,然后在写一个动画让其改变距离最终两个半圆靠拢在一起形成圆
71-
72-
```
73-
/**
74-
* 设置圆角矩形过度到圆的动画
75-
* default_two_circle_distance = (w-h)/2
76-
*/
77-
private void set_rect_to_circle_animation() {
78-
animator_rect_to_square = ValueAnimator.ofInt(0, default_two_circle_distance);
79-
animator_rect_to_square.setDuration(duration);
80-
animator_rect_to_square.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
81-
@Override
82-
public void onAnimationUpdate(ValueAnimator animation) {
83-
two_circle_distance = (int) animation.getAnimatedValue();
84-
85-
//在靠拢的过程中设置文字的透明度,使文字逐渐消失的效果
86-
int alpha = 255 - (two_circle_distance * 255) / default_two_circle_distance;
87-
88-
textPaint.setAlpha(alpha);
89-
90-
invalidate();
91-
}
92-
});
93-
}
94-
```
95-
完成上边代码后再来看下效果
96-
97-
98-
![button_2.gif](http://upload-images.jianshu.io/upload_images/2057501-917b6211e11ba7b9.gif?imageMogr2/auto-orient/strip)
99-
100-
101-
## 第三步:让圆上移移动距离。这个移动很好实现,直接改变Y轴方法的坐标就行了,这个很简单就直接看代码吧
102-
```
103-
/**
104-
* 设置view上移的动画
105-
*/
106-
private void set_move_to_up_animation() {
107-
final float curTranslationY = this.getTranslationY();
108-
animator_move_to_up = ObjectAnimator.ofFloat(this, "translationY", curTranslationY, curTranslationY - move_distance);
109-
animator_move_to_up.setDuration(duration);
110-
animator_move_to_up.setInterpolator(new AccelerateDecelerateInterpolator());
111-
}
112-
113-
```
114-
115-
## 第四步:在圆中绘制一个对勾,而且是带动画的对勾,让对勾以动画的形式慢慢绘制出来
116-
如果对相关API不熟悉的话不知道会怎么去实现呐,或许你会想通过绘制线的方式,在对勾起点开始不断改变移动点的坐标进行绘制,那么怎么获取这些点的坐标呐,这里我们使用Path和DashPathEffect两个方法实现,对DashPathEffect不了解的小伙伴可以去查一下文档哦
117-
> DashPathEffect这个类的作用就是将Path的线段虚线化。
118-
构造函数为DashPathEffect(float[] intervals, float offset),其中intervals为虚线的ON和OFF数组,该数组的length必须大于等于2,phase为绘制时的偏移量。
119-
120-
##### 我们先拿到对勾的path路径在对其改变偏移量加上DashPathEffect就能实现动态绘制对勾的效果了,那么怎么计算对勾的起点折点和终点的坐标呐,在网上找了一个不错的图片,如果你的设计师直接把位置给你标明的很详细的话你就省了这些自己计算的麻烦
121-
122-
![对勾的绘制位置.jpg](http://upload-images.jianshu.io/upload_images/2057501-ca86ac42f1ec4b81.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
123-
124-
```
125-
/**
126-
* 绘制对勾
127-
* 下边计算比例是参考网上一些例子加上自己一步一步尝试的出来的比例,仅供参考
128-
* 如果条件允许最好还是让设计师给你标明一下比例哦!
129-
*/
130-
private void initOk() {
131-
//对勾的路径
132-
path.moveTo(default_two_circle_distance + height / 8 * 3, height / 2);
133-
path.lineTo(default_two_circle_distance + height / 2, height / 5 * 3);
134-
path.lineTo(default_two_circle_distance + height / 3 * 2, height / 5 * 2);
135-
136-
pathMeasure = new PathMeasure(path, true);
137-
138-
}
139-
140-
141-
```
142-
```
143-
/**
144-
* 绘制对勾的动画
145-
*/
146-
private void set_draw_ok_animation() {
147-
animator_draw_ok = ValueAnimator.ofFloat(1, 0);
148-
animator_draw_ok.setDuration(duration);
149-
animator_draw_ok.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
150-
@Override
151-
public void onAnimationUpdate(ValueAnimator animation) {
152-
startDrawOk = true;
153-
float value = (Float) animation.getAnimatedValue();
154-
155-
effect = new DashPathEffect(new float[]{pathMeasure.getLength(), pathMeasure.getLength()}, value * pathMeasure.getLength());
156-
okPaint.setPathEffect(effect);
157-
invalidate();
158-
}
159-
});
160-
}
161-
```
162-
再来看效果
163-
164-
![绘制对勾.gif](http://upload-images.jianshu.io/upload_images/2057501-673df7e71c2bccba.gif?imageMogr2/auto-orient/strip)
165-
166-
167-
至此动画分解都已完成,但是机智的你应该已经发现问题了,就是感觉动画播放衔接的不是很好,那么接下来我们就处理这个问题,回到最初的效果图上,矩形变圆角和缩放成圆形是同时进行的,那么我们有什么办法可以实现动画同时播放呐,哈哈,身为老司机的想必已经知道了使用AnimatorSet,他可以播放动画集、顺序播放等,那么我们就开始处理吧
168-
> 我们让矩形变圆角和矩形往中间缩放同时进行,然后圆在上移,最后绘制对勾
169-
170-
```
171-
animatorSet
172-
.play(animator_move_to_up)
173-
.before(animator_draw_ok)
174-
.after(animator_rect_to_square)
175-
.after(animator_rect_to_angle);
176-
```
177-
178-
最终奉上我们自己一步一步完整实现的效果图
179-
180-
![button_animation.gif](http://upload-images.jianshu.io/upload_images/2057501-0d1119721429bf71.gif?imageMogr2/auto-orient/strip)
181-
182-
## 总结:看到这里是不是觉得这样的动画实现起来也不是很复杂嘛,也许你会觉得这样的动画没什么技术含量,实现起来真的没什么难度,何必再此大做文章呐,其实我这里也只是个抛砖引玉的作用,提供一种学习方法,也许今天我们遇到的只是一个简单的动画,可明天如果需要我们去做更复杂的动画呐,我们该怎么处理,怎么分析,怎么实现呐。只要我们把自己的需求分析拆解,把复杂的步骤简单化,分布实现在组合到一起就可以实现自己想要的效果(你要知道炫酷的电影特效也是一帧一帧动画合成的哦)。
6+
# [**仿微信支付宝等风格的支付密码输入框的实现**](https://github.com/lygttpod/AndroidCustomView/blob/master/pay_psd_input_view.md)
7+
![两种样式供你选择](http://upload-images.jianshu.io/upload_images/2057501-3ca764c315dcdea2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/320)
1838

9+
# [**炫酷的进度条**](https://github.com/lygttpod/AndroidCustomView/blob/master/progress.md)
10+
![progress.gif](http://upload-images.jianshu.io/upload_images/2057501-615ad5fe97faf782.gif?imageMogr2/auto-orient/strip)

animation_button.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
### 今天开始记录工作中遇到的需要实现的动画效果实现自定义view动画,后期会有一些列动画设计思路的文章。
2+
3+
> 在这里分享的是设计实现思路,仅供学习使用,让大家拿到稍微复杂点的动画的时候要知道该如何去一步步分解实现,而不是抱怨
4+
下边就先来看看设计需要的效果图及我们最终实现的效果图,毕竟有图有真相嘛!
5+
6+
7+
![99.gif](http://upload-images.jianshu.io/upload_images/2057501-d710bca1e166fbc4.gif?imageMogr2/auto-orient/strip)
8+
9+
10+
##### 其实我刚拿到设计图的时候心想,MD直接给一张gif图不就行了何必这个麻烦呐,随后冷静下来之后(其实就是抱怨之后)想想作为一名Android开发者总不能什么动画都依赖设计师吧,那样的话会显得我们开发者没什么卵用啊,说不定还会被设计师鄙视哦,于是就开始了动画分析及实现之旅。
11+
******
12+
### 通过这个gif动画我们分析出动画过程的实质:
13+
> 一个长方形(或者是圆角长方形)逐渐过渡成为两边是半圆的长方形,于此同时长方形两边向中间靠拢最终形成一个圆,然后圆上升一定高度,最后在圆里边画出对勾(✔).整个动画分解的其实就是这几个部分,那么我们该如何实现呐,不要捉急,继续往下看。
14+
15+
## 第一步:我们要先画出一个圆角矩形吧
16+
```
17+
/**
18+
* 绘制带圆角的矩形
19+
*
20+
* @param canvas 画布
21+
*/
22+
private void draw_oval_to_circle(Canvas canvas) {
23+
24+
//这里是对矩形的位置大小的设置
25+
rectf.left = two_circle_distance;
26+
rectf.top = 0;
27+
rectf.right = width - two_circle_distance;
28+
rectf.bottom = height;
29+
30+
//画圆角矩形
31+
canvas.drawRoundRect(rectf, circleAngle, circleAngle, paint);
32+
33+
}
34+
35+
```
36+
37+
##### 圆角矩形绘制完成之后就是改变圆角半径的大小使其两边形成半圆的效果,那么怎么才能让他成为半圆呐,来看看一张图,若要绘制成半圆效果,那么这个圆的直径就是view自身的高度,那么这个圆的半径就是height/2
38+
39+
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2057501-741dbdfc3aa6c568.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
40+
41+
42+
```
43+
44+
/**
45+
* 设置矩形过度圆角矩形的动画
46+
*/
47+
private void set_rect_to_angle_animation() {
48+
animator_rect_to_angle = ValueAnimator.ofInt(0, height / 2);
49+
animator_rect_to_angle.setDuration(duration);
50+
animator_rect_to_angle.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
51+
@Override
52+
public void onAnimationUpdate(ValueAnimator animation) {
53+
circleAngle = (int) animation.getAnimatedValue();
54+
invalidate();
55+
}
56+
});
57+
}
58+
59+
```
60+
添加动画之后的效果如下
61+
62+
![button_1.gif](http://upload-images.jianshu.io/upload_images/2057501-e863b04adb9f2440.gif?imageMogr2/auto-orient/strip)
63+
64+
65+
## 第二步:当矩形两边都是半圆之后就要处理使其向中间靠拢逐渐形成一个圆,那么问题又来了,需要向中间移动多少呐,并且怎么移动才能使两边都想中间聚拢呐
66+
##### 下边来看一张图分析一下
67+
68+
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2057501-4f0ded1fc136ae56.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
69+
70+
##### 有图可知移动的距离是(width-height)/2,然后在写一个动画让其改变距离最终两个半圆靠拢在一起形成圆
71+
72+
```
73+
/**
74+
* 设置圆角矩形过度到圆的动画
75+
* default_two_circle_distance = (w-h)/2
76+
*/
77+
private void set_rect_to_circle_animation() {
78+
animator_rect_to_square = ValueAnimator.ofInt(0, default_two_circle_distance);
79+
animator_rect_to_square.setDuration(duration);
80+
animator_rect_to_square.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
81+
@Override
82+
public void onAnimationUpdate(ValueAnimator animation) {
83+
two_circle_distance = (int) animation.getAnimatedValue();
84+
85+
//在靠拢的过程中设置文字的透明度,使文字逐渐消失的效果
86+
int alpha = 255 - (two_circle_distance * 255) / default_two_circle_distance;
87+
88+
textPaint.setAlpha(alpha);
89+
90+
invalidate();
91+
}
92+
});
93+
}
94+
```
95+
完成上边代码后再来看下效果
96+
97+
98+
![button_2.gif](http://upload-images.jianshu.io/upload_images/2057501-917b6211e11ba7b9.gif?imageMogr2/auto-orient/strip)
99+
100+
101+
## 第三步:让圆上移移动距离。这个移动很好实现,直接改变Y轴方法的坐标就行了,这个很简单就直接看代码吧
102+
```
103+
/**
104+
* 设置view上移的动画
105+
*/
106+
private void set_move_to_up_animation() {
107+
final float curTranslationY = this.getTranslationY();
108+
animator_move_to_up = ObjectAnimator.ofFloat(this, "translationY", curTranslationY, curTranslationY - move_distance);
109+
animator_move_to_up.setDuration(duration);
110+
animator_move_to_up.setInterpolator(new AccelerateDecelerateInterpolator());
111+
}
112+
113+
```
114+
115+
## 第四步:在圆中绘制一个对勾,而且是带动画的对勾,让对勾以动画的形式慢慢绘制出来
116+
如果对相关API不熟悉的话不知道会怎么去实现呐,或许你会想通过绘制线的方式,在对勾起点开始不断改变移动点的坐标进行绘制,那么怎么获取这些点的坐标呐,这里我们使用Path和DashPathEffect两个方法实现,对DashPathEffect不了解的小伙伴可以去查一下文档哦
117+
> DashPathEffect这个类的作用就是将Path的线段虚线化。
118+
构造函数为DashPathEffect(float[] intervals, float offset),其中intervals为虚线的ON和OFF数组,该数组的length必须大于等于2,phase为绘制时的偏移量。
119+
120+
##### 我们先拿到对勾的path路径在对其改变偏移量加上DashPathEffect就能实现动态绘制对勾的效果了,那么怎么计算对勾的起点折点和终点的坐标呐,在网上找了一个不错的图片,如果你的设计师直接把位置给你标明的很详细的话你就省了这些自己计算的麻烦
121+
122+
![对勾的绘制位置.jpg](http://upload-images.jianshu.io/upload_images/2057501-ca86ac42f1ec4b81.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
123+
124+
```
125+
/**
126+
* 绘制对勾
127+
* 下边计算比例是参考网上一些例子加上自己一步一步尝试的出来的比例,仅供参考
128+
* 如果条件允许最好还是让设计师给你标明一下比例哦!
129+
*/
130+
private void initOk() {
131+
//对勾的路径
132+
path.moveTo(default_two_circle_distance + height / 8 * 3, height / 2);
133+
path.lineTo(default_two_circle_distance + height / 2, height / 5 * 3);
134+
path.lineTo(default_two_circle_distance + height / 3 * 2, height / 5 * 2);
135+
136+
pathMeasure = new PathMeasure(path, true);
137+
138+
}
139+
140+
141+
```
142+
```
143+
/**
144+
* 绘制对勾的动画
145+
*/
146+
private void set_draw_ok_animation() {
147+
animator_draw_ok = ValueAnimator.ofFloat(1, 0);
148+
animator_draw_ok.setDuration(duration);
149+
animator_draw_ok.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
150+
@Override
151+
public void onAnimationUpdate(ValueAnimator animation) {
152+
startDrawOk = true;
153+
float value = (Float) animation.getAnimatedValue();
154+
155+
effect = new DashPathEffect(new float[]{pathMeasure.getLength(), pathMeasure.getLength()}, value * pathMeasure.getLength());
156+
okPaint.setPathEffect(effect);
157+
invalidate();
158+
}
159+
});
160+
}
161+
```
162+
再来看效果
163+
164+
![绘制对勾.gif](http://upload-images.jianshu.io/upload_images/2057501-673df7e71c2bccba.gif?imageMogr2/auto-orient/strip)
165+
166+
167+
至此动画分解都已完成,但是机智的你应该已经发现问题了,就是感觉动画播放衔接的不是很好,那么接下来我们就处理这个问题,回到最初的效果图上,矩形变圆角和缩放成圆形是同时进行的,那么我们有什么办法可以实现动画同时播放呐,哈哈,身为老司机的想必已经知道了使用AnimatorSet,他可以播放动画集、顺序播放等,那么我们就开始处理吧
168+
> 我们让矩形变圆角和矩形往中间缩放同时进行,然后圆在上移,最后绘制对勾
169+
170+
```
171+
animatorSet
172+
.play(animator_move_to_up)
173+
.before(animator_draw_ok)
174+
.after(animator_rect_to_square)
175+
.after(animator_rect_to_angle);
176+
```
177+
178+
最终奉上我们自己一步一步完整实现的效果图
179+
180+
![button_animation.gif](http://upload-images.jianshu.io/upload_images/2057501-0d1119721429bf71.gif?imageMogr2/auto-orient/strip)
181+
182+
## 总结:看到这里是不是觉得这样的动画实现起来也不是很复杂嘛,也许你会觉得这样的动画没什么技术含量,实现起来真的没什么难度,何必再此大做文章呐,其实我这里也只是个抛砖引玉的作用,提供一种学习方法,也许今天我们遇到的只是一个简单的动画,可明天如果需要我们去做更复杂的动画呐,我们该怎么处理,怎么分析,怎么实现呐。只要我们把自己的需求分析拆解,把复杂的步骤简单化,分布实现在组合到一起就可以实现自己想要的效果(你要知道炫酷的电影特效也是一帧一帧动画合成的哦)。
183+

0 commit comments

Comments
 (0)