|
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
Adapter是listview和数据源间的中间人。
当每条数据进入可见区域时,adapter的getview()会被调用,返回代表具体数据的视图。触摸滚动时,频繁调用。支持成百上千条数据。
下面为显示每条数据的xml文件:
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"horizontal"
>
<ImageView android:id=
"@+id/icon"
android:layout_width=
"48dip"
android:layout_height=
"48dip"
/>
<TextView android:id=
"@+id/text"
android:layout_gravity=
"center_vertical"
android:layout_width=
"0dip"
android:layout_weight=
"1.0"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
1
。最简单的方法,最慢且最不实用
public
View getView(
int
pos, View convertView,
ViewGroup parent){
View item = mInflater.inflate(R.layout.list_item,
null
);
((TextView) item.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) item.findViewButId(R.id.icon)).
setImageBitmap((pos &
1
) ==
1
? mIcon1 : mIcon2);
return
item;
}
2
。利用convertview回收视图,效率提高
200
%。
public
View getView(
int
pos, View convertView,
ViewGroup parent){
if
(convertView ==
null
) {
convertView = mInflater.inflate(
R.layout.list_item,
null
);
}
((TextView) convertView.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) convertView.findViewButId(R.id.icon)).
setImageBitmap((pos &
1
) ==
1
? mIcon1 : mIcon2);
return
convertView;
}
3
。利用viewholder模式,效率在提高
50
%
static
class
ViewHolder {
TextView text;
ImageView icon;
}
public
View getView(
int
pos, View convertView, ViewGroup parent){
ViewHolder holder;
if
(convertView ==
null
) {
convertView = mInflater.inflate(R.layout.list_item,
null
);
holder =
new
ViewHolder();
holder.text = (TextView) convertView.findViewById(
R.id.text));
holder.icon = (ImageView) convertView.findViewButId(
R.id.icon));
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos &
1
) ==
1
? mIcon1 : mIcon2);
return
convertView;
}
adapter更新效率比较:
1
的更新不到
10
frames/second
2
的更新接近
30
frames/second
3
的更新接近
40
frames/second
背景和图像
视图背景图像总会填充整个视图区域
1
。图像尺寸不合适会导致自动缩放
2
。避免实时缩放
3
。最好预先缩放到视图大小
originalImage = Bitmap.createScaledBitmap(
originalImage,
// 缩放图像
view.getWidth(),
// 视图宽度
view.getHeight(),
// 视图高度
true
);
// 线性过滤器
1
的效率接近
25
frames/second
2
的效率接近
50
frames/second
默认情况下, 窗口有一个不透明的背景
有时可以不需要
-最高层的视图是不透明的
- 最高层的视图覆盖整个窗口
layout_width = fill_parent
layout_height = fill_parent
更新看不见的背景是浪费时间
删除窗口背景:
1
。修改编码
public
void
onCreate(Bundle icicle){
super
.onCreate(icicle);
setContentView(R.layout.mainview);
// 删除窗口背景
getWindow().setBackgroundDrawable(
null
);
...
}
2
。修改xml
首先确定你的res/values/styles.xml有
<resources>
<style name=
"NoBackgroundTheme"
parent=
"android:Theme"
>
<item name=
"android:windowBackground"
>
@null
</item>
</style>
</resources>
然后编辑androidmainfest.xml
<activity android:name=
"MyApplication"
android:theme=
"@style/NoBackgroundTheme"
>
...
</activity>
更新请求
当屏幕需要更新时,调用invalidate()方法,简单方便,但是更新了整个视图,代价太高。
最好先找到无效区域,然后调用
invalidate(Rect dirty);
invalidate(
int
left,
int
top,
int
right,
int
bottom);
视图和布局
如果一个窗口包含很多视图,启动太慢,绘制时间长,用户界面反应速度很慢
解决方法:
1
。使用textview的复合drawable减少层次
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"@string/hello"
android:drawableLeft=
"@drawable/icon"
/>
2
。使用viewstuf延迟展开视图
在xml文件中定义viewstuf
<ViewStub android:id =
"@+id/stub_import"
android:inflatedId=
"@+id/panel_import"
android:layout=
"@layout/progress_overlay"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom"
/>
在需要展开视图时,
findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
// 或者
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();
3
。使用<merge>合并中间视图
默认情况下,布局文件的根作为一个节点,加入到父视图中,如果使用merge可以避免根节点
<merge xmlns:android =
"http://schemas.android.com/apk/res/android"
>
<! -- Content -->
</merge>
4
。使用ralativelayout减少层次
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
>
<ImageView android:id=
"@+id/icon"
android:layout_width=
"48dip"
android:layout_height=
"48dip"
android:layout_alignParentLeft=
"true"
android:layout_centerVertical=
"true"
/>
<TextView android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/text_line1"
android:layout_alignParentTop=
"true"
android:layout_toRightOf=
"@id/icon"
/>
<TextView android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/text_line2"
android:layout_toRightOf=
"@id/icon"
android:layout_below=
"@id/text_line1"
/>
<Checkbox android:id=
"@+id/star"
android:layout_width=
"48dip"
android:layout_height=
"48dip"
android:layout_alignParentRight=
"true"
android:layout_centerVertical=
"true"
/>
</RelativeLayout>
5
.使用自定义视图
class
CustomView
extends
View {
@Override
protected
void
onDraw(Canvas canvas) {
// 加入你的绘图编码
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
// 计算视图的尺寸
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
6
使用自定义布局
class
GridLayout
extends
ViewGroup {
@Override
protected
void
onLayout(
boolean
changed,
int
l,
int
t,
int
r,
int
b) {
final
int
count = getChildCount();
for
(
int
i=
0
; i < count; i++) {
final
View child = getChildAt(i);
if
(child.getVisibility() != GONE) {
// 计算子视图的位置
child.layout(left, top, right, bottom);
}
}
}
}
内存分配
在性能敏感的代码里,避免创建java对象
1
。测量 onmeasure()
2
。布局onlayout()
3
。绘图 ondraw() dispatchdraw()
4
。事件处理 ontouchevent() dispatchtouchevent()
5
。adapter: getview() bindview()
强行限制(适用调试模式)
int
prevLimit = -
1
;
try
{
prevLimit = Debug.setAllocationLimit(
0
);
// 执行不分配内存的代码
}
catch
(dalvik.system.AllocationLimitError e) {
// 如果代码分配内存, Java 虚拟机会抛出错误
Log.e(LOGTAG, e);
}
finally
{
Debug.setAllocationLimit(prevLimit);
}
管理好对象:
1
。适用软引用:内存缓存的最佳选择
2
。适用弱引用:避免内存泄露
内存缓存:
private
final
HashMap<String, SoftReference<T>> mCache;
public
void
put(String key, T value) {
mCache.put(key,
new
SoftReference<T>(value));
}
public
T get(String key, ValueBuilder builder) {
T value =
null
;
SoftReferece<T> reference = mCache.get(key);
if
(reference !=
null
) {
value = reference.get();
|
提高android应用的效率--主要讲解listview的优化
微信关注我们
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
相关文章
发表评论
资源下载
更多资源腾讯云软件源
为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。
Nacos
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Spring
Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。
Rocky Linux
Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。