Skip to content

Commit 9343f40

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 6b6cacd + 5b30609 commit 9343f40

1 file changed

Lines changed: 81 additions & 54 deletions

File tree

README.md

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# RandomTextView
2-
滚动显示TextView的数字,支持自定义每个字符速度。
1+
本文已经在微信公众号【Android群英传】独家发表。
2+
33

4-
感觉可以一用,一定要顺手star我哦~
4+
未经允许不得转载。
5+
转载请注明作者AndroidMsky及原文链接
6+
http://blog.csdn.net/androidmsky/article/details/53009886
7+
本文Github代码链接
8+
https://github.com/AndroidMsky/RandomTextView
59

6-
最近在掘金这个干货平台上发了几篇博文,[这里进入我的掘金主页](http://gold.xitu.io/user/580f1397da2f60004f422d18)
10+
2016年11-30号,一位热心同学私信我反映会出现内存泄漏问题。特别推出v1.2检测并且,解决内存泄漏问题,并讲述一下,看过本文的直接点传送门
711

8-
看掘金APP中文章数据的数字滚动起来很动感,效果很棒,
12+
[2.v1.2更新内容](#2)
913

10-
于是决定把它通过自定义View编写出来,方便自己和大家调用。
14+
Github代码已经更新为v1.2
1115

12-
原文链接
13-
http://blog.csdn.net/androidmsky/article/details/53009886
1416

15-
作者博客地址:
16-
http://blog.csdn.net/androidmsky?viewmode=list
17+
2016年11月11号,RandomTextView第一次更新为v1.1版本吧。
18+
(解决了这样一个场景,一个抽奖的页面想滚动30秒,可能maxline加到100行的数字滚动,对此我要对性能进行优化避免过度绘制,在本文最后做出解释)
1719

18-
先看看掘金的效果:
20+
Github代码已经更新为v1.1
1921

20-
![这里写图片描述](http://img.blog.csdn.net/20161102161400896)
2122

23+
[1.v1.1更新内容](#1)
2224

23-
我们自己实现的效果
25+
先看看X金APP的效果
2426

25-
![这里写图片描述](http://img.blog.csdn.net/20161103091026709)
27+
![这里写图片描述](http://img.blog.csdn.net/20161102161400896)
2628

2729

30+
我们自己实现的效果:
2831

29-
感觉可以一用,一定要star我哦~
3032

33+
![这里写图片描述](http://img.blog.csdn.net/20161102161502895)
3134

3235
接下来介绍一下我的自定义View RandomTextView的用法和原理
3336

34-
2016年11月11号,RandomTextView第一次更新为v1.1版本吧。
35-
(解决了这样一个场景,一个抽奖的页面想滚动30秒,可能maxline加到100行的数字滚动,对此我要对性能进行优化避免过度绘制,在本文最后做出解释)
36-
3737
用法
3838
--
3939
考入
@@ -97,10 +97,18 @@ mRandomTextView.setText("909878");
9797
```
9898
mRandomTextView.setMaxLine(20);
9999
```
100+
放置泄漏
101+
```
102+
@Override
103+
protected void onDestroy() {
104+
super.onDestroy();
105+
mRandomTextView.destroy();
106+
}
107+
```
100108

101109
原理
102110
--
103-
用TextView去绘制10(maxLine可设置)行文字,调用canvas.drawText去绘制出来,在绘制的Y坐标不断增加便宜量,去改变绘制的高度,通过handler.postDelayed(this, 20);不断增加便宜量,并且不断判断所有位数字最后一行绘制完毕的时候,结束handler的循环调用。
111+
用TextView去绘制10(maxLine可设置)行文字,调用canvas.drawText去绘制出来,在绘制的Y坐标不断增加便宜量,去改变绘制的高度,通过handler.postDelayed(this, 20);不断增加偏移量,并且不断判断所有位数字最后一行绘制完毕的时候,结束handler的循环调用。
104112

105113
需要的变量:
106114

@@ -167,7 +175,7 @@ OnDraw方法:
167175
drawNumber(canvas);
168176
169177
```
170-
自一次进入onDraw方法时,做了如下几件事情:
178+
第一次进入onDraw方法时,做了如下几件事情:
171179
**1.**去获取当前正确的画笔p = getPaint();从而保证xml中配置的大小颜色等有效。
172180
**2.**通过当前画笔去计算正确的drawText基准线。
173181
baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
@@ -333,57 +341,76 @@ private final Runnable task = new Runnable() {
333341
334342
}
335343
```
344+
<h2 id="2">v1.2更新内容</h2>
345+
v1.2更新内容:
346+
解决内存泄漏问题,
347+
看到泄可能有点手抖,不过面对现实。
348+
上图:
336349

350+
![这里写图片描述](http://img.blog.csdn.net/20161130174054510)
337351

338-
v1.1更新内容:
339-
340-
之前我们的思路是按照maxLine画出每一行,但是我们最多看见2行内容,这样是不科学的,完全中了过度绘制的圈套呀,再想如下一个场景,一个抽奖的页面想滚动30秒,可能maxline加到100行的数字滚动,那每帧都要绘制100行的text这显然会出现性能问题,造成掉帧的影响,所以我们队drawtext方法进行一下拦截,新建一个drawText方法:
352+
如果反复选择屏幕让Activty重新创建,就会出现内存泄漏,安利给大家内存泄漏检测工具:leakcanary:https://github.com/square/leakcanary
353+
配置十分简单先是引用:(2016.11.30版本)
341354

342355
```
343-
private void drawText(Canvas mCanvas,String text,float x,float y,Paint p){
344-
345-
if (y>=-measuredHeight&&y<=2*measuredHeight)
346-
347-
mCanvas.drawText(text + "", x,
348-
y, p);
349-
else return;
350-
}
356+
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
357+
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
358+
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
351359
```
352-
我们对y坐标进行判断,如果在textView上下各一个textView大小内,我们进行绘制,如果超出这个范围我们直接return,不做任何处理,这样既不影响我们的绘制逻辑又解决了过渡绘制问题。
353-
讲原来的drawText方法替换:
360+
然后:
354361

355362
```
356-
drawText(canvas,arrayListText.get(j) + "", 0 + f0 * j,
357-
baseline, p);
358-
// canvas.drawText(arrayListText.get(j) + "", 0 + f0 * j,
359-
// baseline, p);
360-
```
361-
作者将持续维护该框架,也希望大家star,fork,issue。
362-
363-
共同做出一个更好的RandomTextView
364-
365-
2016.11.11 Androidmsky
366-
363+
public class MyApplication extends Application {
367364
365+
@Override
366+
public void onCreate() {
367+
super.onCreate();
368+
if (LeakCanary.isInAnalyzerProcess(this)) {
369+
// This process is dedicated to LeakCanary for heap analysis.
370+
// You should not init your app in this process.
371+
return;
372+
}
373+
LeakCanary.install(this);
374+
}
375+
}
368376
377+
```
378+
如果检测activity的泄漏问题,可以开启旋转屏幕一旋转就重新创建activity了,这样就反反复复创建activity。如上图泄漏问题就会被推送出来,而且明确告诉你是什么样一个引用链导致的泄漏。工具很强大有么有。本文框架的问题就是,如果RandomTextview的动画没有停止,那么activity就不会被释放掉,这样就造成了泄漏,所以在activity中写入:
369379

380+
```
370381
382+
@Override
383+
protected void onDestroy() {
384+
super.onDestroy();
385+
mRandomTextView.destroy();
386+
}
387+
```
388+
并且提供destroy方法:
371389

372-
绘制原理的逻辑就讲完啦,RandomTextView可以投入使用啦,自定义view并不难,只要你知道安卓API能让你能干什么,你想干什么,你可能马上就知道你应该怎么做啦。
390+
```
391+
public void destroy (){
392+
auto=false;
393+
handler.removeCallbacks(task);
373394
374-
欢迎关注作者。欢迎评论讨论。欢迎拍砖。
395+
}
396+
```
375397

376-
如果觉得这篇文章对你有帮助 欢迎打赏,
398+
欢迎大家提出各种问题,让控件越来越好用谢谢。
399+
2016.11.30 Androidmsky
400+
## License
377401

378-
欢迎star,Fork我的github。
402+
Copyright 2016 AndroidMsky
379403

380-
喜欢作者的也可以Follow。也算对作者的一种支持。
381-
本文Github代码链接
382-
https://github.com/AndroidMsky/RandomTextView
404+
Licensed under the Apache License, Version 2.0 (the "License");
405+
you may not use this file except in compliance with the License.
406+
You may obtain a copy of the License at
383407

384-
欢迎加作者自营安卓开发交流群:308372687
385-
![这里写图片描述](http://img.blog.csdn.net/20161028111556438)
408+
http://www.apache.org/licenses/LICENSE-2.0
386409

410+
Unless required by applicable law or agreed to in writing, software
411+
distributed under the License is distributed on an "AS IS" BASIS,
412+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
413+
See the License for the specific language governing permissions and
414+
limitations under the License.
387415

388416

389-
博主原创未经允许不许转载。

0 commit comments

Comments
 (0)