2017-10-18 69 views
1

目前我正试图在活动的顶部设置背景图片。 规格的图像:背景图片缓慢的应用程序下降

高度: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它运行平稳。

+0

如果将'android:background =“#E6E6E6”'更改为'“@ drawable/background_rocket”',是否有区别? – pskink

+0

@pskink不,不变。但是,谢谢。 – MyNewName

+0

@pskink是的,我正在使用自定义的'Adapater'。 'GetView'方法的内容:'if(convertView == null) convertView = mContext.getLayoutInflater()。inflate(R.layout.list_item_person,null);返回convertView;' – MyNewName

回答

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); 
+0

感谢您的答案。我试了一下。现在有一些不同。当我看到图像是放养的时候,但是当图像滚动出来时,它就可以工作。 – MyNewName

+0

由于图片加载需要一点时间 – KeLiuyue

1

如果有750x600分辨率的图像是在drawable-xxxhdpi文件夹,该影像将在三星S8缩放。因为您的设备具有1440x2960像素分辨率。

缩放过程自然会增加创建时间过程,也将消耗大约5 MB从存储器,未45 kb的

将较小的图像缩放到较大的位置会在一些较大的屏幕设备上导致内存问题(例如OOM异常)。

而且你的ListView项目上的图像也将被缩放。这种缩放过程可能是laggy滚动问题的原因。

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