现在的位置: 首页 > 综合 > 正文

Android 软件优化

2013年12月04日 ⁄ 综合 ⁄ 共 3774字 ⁄ 字号 评论关闭


Android软件开发中常见的几个瓶颈

不同分辨率适配

不同版本调试

语言?性能?触摸?动画?

内存,内存,内存...

Android软件内存限制

Android系统对每个软件所能使用的RAM空间进行了限制(: Nexus one 对每个软件的内存限制是24M)

 

Java语言本身比较消耗内存

 

dalvik虚拟机也要占用一定的内存空间

OOM功臣—Bitmap

当在Android手机上直接读取4M的图片时,死神一般都会降临

往往自己手机拍摄的照片都不能直接读取

 

 

 

 

OOM功臣—Bitmap

技巧:读取图片之前先查看其大小

 

BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

 

使用得到的图片原始宽高计算适合自己的smaplesize

 

BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inSampleSize = 4;

Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

OOM功臣—Bitmap

对于过时的Bitmap对象一定要及时recycle

并且把此对象赋值为null

bitmap.recycle();

bitmap = null; 

View优化

使用9patch格式的图片资源

 

使用png图片压缩工具对9patch格式图片压缩

 

使用-land, -mdpi, -hdpi等作为layout和当然啊wable的后缀,来做图片适应和不同分辨率适配

View优化

使用layoutopt进行优化

$ layoutoptsamples/ samples/compound.xml

The root-level <FrameLayout/> can be replaced with <merge/>

This LinearLayoutlayout or its FrameLayoutparent is useless samples/simple.xml

The root-level <FrameLayout/> can be replaced with <merge/> samples/too_deep.xml

This layout has too many nested layouts: 13 levels, it should have <= 10!

 

参考http://android-developers.blogspot.com/2009/11/optimize-your-layouts.html

减少逻辑代码工作量

 

class MyActivityextends Activity {

public void myClickHandler(View v) {

// Do stuff

} }

Layout xml文件中加入:

<Button android:onClick="myClickHandler" />

即可实现不在代码中新建OnClickListener

使用最少的View

 

 

 

LinearLayoutxmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“fill_parent” android:layout_height=“?android:attr/listPreferredItemHeight” android:padding=“6dip”>

 <ImageViewandroid:id=“@+id/icon” android:layout_width=“wrap_content” android:layout_height=“fill_parent” android:layout_marginRight=“6dip” android:src=“@drawable/icon” />

<LinearLayoutandroid:orientation=“vertical” android:layout_width=“0dip” android:layout_weight=“1” android:layout_height=“fill_parent”>

<TextViewandroid:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" android:text="My Application" />

<TextViewandroid:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:singleLine="true" android:ellipsize="marquee" android:text="Simple application that shows how to use RelativeLayout" />

 </LinearLayout>

 </LinearLayout>

去掉不必要的View

 

<resources>

<style name="Theme.NoBackground" parent="android:Theme">

<item name="android:windowBackground">@null</item> </style>

</resources>

Manifest.xmlActivityApplication中添加android:theme="@style/Theme.NoBackground"

减小图片大小

res/drawable/background_shelf.xml中使用:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/shelf_panel" android:tileMode="repeat" />

使用Stub

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime

<ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree"   android:layout="@layout/mySubTree" android:layout_width="120dip" android:layout_height="40dip" />

 ViewStub stub = (ViewStub) findViewById(R.id.stub);

 View inflated = stub.inflate();

代码级内存优化

 Android提供了很健全的消息传递机制(Intent)和任务模型(Handler),可以通过传递或事件的方式,防止一些不必要的全局变量

 

对于很多必不可少的全局引用,如果不是随时需要或者必不可少,则可以使用SoftRerence 或者 WeakRerence引用他们,可以保证在系统内存不足的时候,他们会首先释放而不是出现OOM

 

如果有牵涉到大量对象或者组合数据的算法操作,则最好使用C语言封装这些对象并进行操作,通过jni调用至于java语言进行最简单的数据通信。因为即使空的JAVA语言对象也会占据20多个字节

Android软件性能优化浅谈

Android性能杀手:

1. GC, 系统GC之低效

 

2. layout布局和view显示

 

3. load图片和文件读取

Android软件性能优化浅谈

提升性能的几点技巧:

1. 减少临时对象的频繁生成和销毁

 

2. 使用ViewStub延迟加载某些比较复杂的layout

 

3. 循环或者多层循环内的操作尽可能简单,更能享受jit带来的加速

 

4. 占用CPU较多的数据操作尽可能放在一个单独的线程中进行,通过handler等方式把执行的结果交于UI线程显示

小结

Android系统中处处可见生命周期的使用,包括activityview等等。此种做法在线程同步等方面有很积极的意义,并且对程序执行的阶段有很清晰的划分。

我们只要严格按照Android程序执行的生命周期,在处理内存消耗型任务时,使用压缩,缓冲和流等方式,在处理cpu消耗型任务时采用额外线程处理,UI线程展示的方式,就可以在编写Android程序时避免很多古怪的问题。

Own Website:http://www.stay4it.com/

QQ:104816053

Android QQ Group: 点击加入

转载请注明出处http://stay.cnblogs.com/ 请尊重个人劳动成果。

from : http://www.cnblogs.com/stay/archive/2011/09/01/2162595.html

抱歉!评论已关闭.