背景图片缓慢的应用程序下降
问题描述:
目前我正试图在活动的顶部设置背景图片。 规格的图像:背景图片缓慢的应用程序下降
高度:290dp 宽度:match_parent ScaleType:cropCenter
背景图像被存储在@drawables。图像的大小为750 x 600像素。 这是41.3 KB大。
我活动的XML看起来是这样的:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6">
<ImageView
android:layout_height="290dp"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background_rocket">
</ImageView>
<ListView
android:id="@+id/list"
android:paddingTop="240dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:divider="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:descendantFocusability="blocksDescendants">
</ListView>
</RelativeLayout>
这正是我想要的,但它是可怕的慢(当我上运行它我的S8)。当我向下滚动时,它会卡住。当我在Android Studio模拟器上运行它时,它可以正常工作(我认为速度稍慢一些,但在真实手机上要好得多)。
我已经下载了插件:Drawable Importer。
但它并没有帮助。
注意:自定义ListView Adapter
应该没问题,因为当我删除ImageView
它运行平稳。
答
将ImageView
作为ListView
的标题。
试试这个。
ListView listView = (ListView) findViewById(R.id.your_listview);
View headerView = LayoutInflater.from(this).inflate(R.layout.your_image,null);
listView.addHeaderView(headerView);
答
如果有750x600
分辨率的图像是在drawable-xxxhdpi
文件夹,该影像将在三星S8缩放。因为您的设备具有1440x2960
像素分辨率。
缩放过程自然会增加创建时间过程,也将消耗大约5 MB从存储器,未45 kb的。
将较小的图像缩放到较大的位置会在一些较大的屏幕设备上导致内存问题(例如OOM异常)。
而且你的ListView项目上的图像也将被缩放。这种缩放过程可能是laggy滚动问题的原因。
注意:始终使用RecyclerView以获得更好的性能和更大的灵活性。 对于图片加载,您可以使用Glide或Picasso库提供更好的性能和更好的缓存机制。
如果将'android:background =“#E6E6E6”'更改为'“@ drawable/background_rocket”',是否有区别? – pskink
@pskink不,不变。但是,谢谢。 – MyNewName
@pskink是的,我正在使用自定义的'Adapater'。 'GetView'方法的内容:'if(convertView == null) convertView = mContext.getLayoutInflater()。inflate(R.layout.list_item_person,null);返回convertView;' – MyNewName