@SuppressLint("DrawAllocation")作用和解决方法
来源:互联网 发布:linux dr.com客户端 编辑:程序博客网 时间:2024/06/03 23:42
在Android自定义控件中,经常会遇到这样的一些警告(如下图),这些警告如果不去处理的话,并不会影响我们程序的正常运行,但是总有强迫症的人看了会觉得很不爽,比如我就是其中一个。
其实,既然程序会报出这样的警告,肯定就会有他的不合理之处,只是并不会导致程序发生错误无法运行,我们写的代码平时也不多,所以一般对我们的程序不会有多大的影响,但是他会影响整个程序的安全性及一些其他性能,所以我们还是尽量去避免这写不合理之处。
警告原因:
首先,我们从警告的提示来看,Avoid object allocations during draw/layout operations (preallocate and reuse instead),意为避免在绘制/布局中去实例化对象。这样警告就很明显了,为什么会这样呢?
因为在View及其子类的onDraw(Canvas canvas)方法,会实时调用以更新界面,会频繁的创建对象和进行垃圾回收等,这明显就会影响UI的显示性能,这样一个显示很顺畅的用户界面就会因对象分配引起的一些垃圾回收机制进行短暂的停滞。
解决方法
具体的解决方法也很简单,我们只需要将创建对象等这些分配内存资源和会引起垃圾回收机制的操作在之前进行,例如设置为全局变量,提取一个init()方法来实例化对象等,这样就会解决这些问题了。
例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
PS: 这些小问题看似影响不大,但是时间久了,或者累积多了,就会使程序性能下降,甚至导致程序奔溃,尽量去避免他们。
阅读全文
0 0
- @SuppressLint("DrawAllocation")作用和解决方法
- @SuppressLint("DrawAllocation")作用和解决方法
- @SuppressLint("DrawAllocation")作用
- android TargetApi 和 SuppressLint
- TargetAPI和SuppressLint
- android 基础 @SuppressLint("NewApi")和@TargetApi()的作用与区别
- @SuppressLint
- @SuppressLint("NewApi")的作用
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi(number)使用比较
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- Python之redis(二)
- 时间序列笔记(六)
- org.springframework.data.mapping.PropertyReferenceException: No property name found for type User
- 对数字的处理,得到位数,逆序输出,顺序输出
- 基于.NET平台常用的框架整理
- @SuppressLint("DrawAllocation")作用和解决方法
- 图像分割
- 微信H5牛牛搭建教程
- 二维数组的问题
- 配合七牛云简易使用kindeditor编辑器的图片上传功能
- 在linux服务器上安装单机版redis
- String 和StringBuilder、StringBuffer 的区别
- python基础语法中所遇到的错误整理
- String,StringBuffer与StringBuilder的区别