背景图像慢app下来

时间:2017-10-18 06:58:23

标签: android image imageview

目前我正在尝试在活动的顶部设置背景图片。 图像规格:

高度: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>

正在运行的应用程序的结果: enter image description here

这正是我想要的,但它很慢(当我在S8上运行时)。当我向下滚动时,它会卡住。当我在Android Studio模拟器上运行时,它运行正常(我认为正常情况下有点慢,但在我的真实手机上要好得多)。

我已经下载了插件:Drawable Importer。

enter image description here

但它没有帮助。

注意:自定义ListView Adapter应该没有问题,因为当我删除ImageView时,它会顺利运行。

3 个答案:

答案 0 :(得分:1)

ImageView添加为ListView的标题。

试试这个。

ListView listView = (ListView) findViewById(R.id.your_listview);
View headerView = LayoutInflater.from(this).inflate(R.layout.your_image,null);
listView.addHeaderView(headerView);

答案 1 :(得分:1)

如果分辨率为750x600的图片位于drawable-xxxhdpi文件夹中,则图片将在Samsung S8上缩放。因为您的设备的像素分辨率为1440x2960

缩放过程会自然地增加创建时间过程,也会从内存中消耗 5 mb ,而不是 45 kb

将较小的图像缩放到较大的图像会导致某些较大的屏幕设备出现内存问题(例如OOM异常)。

此外,ListView项目上的图像也会缩放。此缩放过程可能是导致延迟滚动问题的原因。

注意:始终使用RecyclerView以获得更好的性能和更大的灵活性。 对于图像加载,您可以使用 Glide Picasso 库来获得更好的性能和更好的缓存机制。

答案 2 :(得分:0)

尝试使用Universal Image LoaderPicasso或任何其他图像加载库等任何图像加载器加载图像

相关问题