android内存管理优化

//内存缓存实例
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;
softReference<T> reference =mCache.get(key);
if(reference !=null){
value =reference.get();
}
if(value == null){
value=builder.build(key);
mCache.put(key,new Softreference<T>(value));
}
return value;
}

/*Adapter 时listview和数据源的中间人
当每条数据进入可见区时,adapter的getView()会被调用,并
返回代表具体数据的视图,触摸滚动时频繁调用,
支持成百上千条数据

*/
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.findViewById(R.id.icon)).setImageBitmap((pos&1)==1?mIcon1:mIcon2);
return item;
}//最简单的方法,最慢最不适用

//利用convertView回收视图,效率提高200%
if(convertview==null){
converview = mInflater.inflate(R.layout.list_item,null);

}
((TextView)converview.findViewById(R.id.text)).setText(Data[pos]);

//使用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.findViewById(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;
}



//背景和图片
最好预先缩放到视图大小
originalImage=Bitmap.createScaleBitmap(
originalImage,//被缩放的图片
view.getWidth(),//视图宽
view.getHeight(),//视图高
true);//双线性过滤


删除窗口背景
修改编码  getWindow().setBackgroundDrawable(null);
修改xml声明
在res/values/styles.xml中定义
<resources>
<style name ="NoBackgroundTheme" parent="android:Theme">
<item name="android:windowBackground">@null</item>
</style>
<resources>
然后编辑AndrMainfest.xml
<Activity android:name="mactivity"
android:theme="@style/NoBackgroundTheme">


更新屏幕局部无效区域
invalidate(Rest dirty);
invalidate(int left,int top,int right, int bottom);


视图和布局,越简单越好
使用TextView的复合drawables减少层次
<Textview 
android:layout_width="warp_content"
android:layout_height="warp_content"
android:text="@string/hello"
android:drawableLeft="@drawable/icon"/>
使用viewStub延迟展开视图
使用<merge>合并中间视图
<merge xmlns:android="http://schemas.android.com/apk/res/android">相同的头部或尾部</merge>
<include layout="@layout/head">
使用RelativeLayout减少层次
android:layout_alignParentLeft="true"//相对父窗体对齐父窗体左边
android:layout_alignParentTop="true"//对齐父窗体上面
android:layout_centerVertical="true"//竖直方向居中
android:layout_toRightOf="@id/icon"//icon的右边
android:layout_below="@id/icon"//icon的下面


使用自定义视图和布局
自定义视图
class CustomView extends View{
protected void onDraw(Canvas canvas){
//加入自己的绘图编码
}
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
//
setMeasureDimension(widthSpecSize,heightSpecSize);
}
}
自定义布局
class GridLayout extends viewGroup{
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对象
测量 onMeasure();
布局 onLayout();
绘图 dispatchDraw(),onDraw();
事件处理dispatchTouchEvent(),onTouchEvent(),
适配器:getView(),bindView(),
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值