我在一个界面里,将根布局设置background,设置为一张400多k的图片,然后发现当进入这个activity的时候,内存一下子就涨了40多m,之前如果不加bakground的话,内存基本上不动?
我知道图片很占内存,可是这也太夸张了吧,还有,有没有什么好的方法来解决?
如果根布局设置了背景,子布局不需要的情况下就设置background="#00000000",优化 UI绘制。其实你可以贴下相关代码这样好分析一点。
先看看具体什么占用内存了!并不一定是图片占用,可能是相关处理代码问题
可以把代码贴出来看下,没道理这么夸张的,应该是代码哪里有问题
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/clubbg"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:paddingLeft="25dp"
android:paddingRight="25dp">
<com.fc.ui.view.XCRoundImageView
android:id="@+id/img_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="@drawable/headframe"
android:src="@drawable/head" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignLeft="@+id/spinner"
android:layout_alignParentRight="@+id/spinner"
android:layout_below="@+id/spinner"
android:background="@color/myButton_color_concrete" />
<TextView
android:id="@+id/txt_arena"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/spinner"
android:layout_marginTop="10dp"
android:text="职业技术学院体育馆" />
<Button
android:id="@+id/img_btn"
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/morebtn"
android:text="i" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/layout_distinction"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/style_club_linear"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="战绩" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="@drawable/clubdistinction" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_member"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/style_club_linear">
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:gravity="center"
android:text="成员" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="@drawable/clubmeb" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/style_club_linear">
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="消息" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="@drawable/clubinfo" />
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="动态"
android:textSize="20sp" />
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:listSelector="#00000000"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:ptrListViewExtrasEnabled="false"
app:ptrScrollingWhileRefreshingEnabled="true" />
楼主解决了没,碰到一样的问题了
background也需要java层去createBitmap 既然是bitmap那就需要按它的机制去分配内存 比如你的图片是1080*1920 那它将需要分配大概7.9M的内存;尺寸越大占用内存也就越大;