在Recycler视图上添加图像视图时,Android导航抽屉动画滞后/减速

问题描述 投票:0回答:2

我在相对布局中有一个recyclerview,应用程序工作正常,导航抽屉很光滑。但是我在Recycler视图上添加了一个Imageview,以便我可以根据数据的可用性进行隐藏和显示。但是,当我添加Imageview时,导航抽屉变得非常慢。当我删除I​​mageview时,它再次顺利运行。图像大小仅为38kb。有人能告诉我如何以有效的方式显示空状态

这是我使用ImageView的布局

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.test.sample.itemHistory"
tools:showIn="@layout/app_bar_item_history"
android:background="#ffffff">

<android.support.v7.widget.RecyclerView
    android:id="@+id/materialList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageViewNoItems"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/noItems" />
</RelativeLayout>

提前致谢

android user-interface layout imageview android-recyclerview
2个回答
0
投票

使用毕加索或滑动库进行图像加载..并在清单中添加一行,您的项目就像以前一样变得平滑。

    <application
       android:largeHeap="true">
    </application>

largeHeap用于在加载图像时避免outofmemory异常。


0
投票

在我的情况下,我尝试将图像(项目可绘制资源文件夹中的PNG文件)加载到我的RecycleView的ImageView以及使用Picasso,我面临同样的问题,导航抽屉滑动动画正在减慢。

我发现主要问题是因为我的图像文件非常大,所以毕加索将全尺寸图像加载到RecycleView的ImageView中会导致滞后。

我的解决方案是:(我使用的是KOTLIN语言)

  1. 使用Picasso内置的调整大小功能重新调整图像大小

没有调整大小(导致滞后):

Picasso.get().load(R.drawable.icon).into(holder.view.ImageView)

调整大小(解决延迟问题):

Picasso.get().load(R.drawable.icon).resize(400,400).centerCrop().into(holder.view.ImageView)
  1. 使用setImageDrawable而不是Picasso

此方法不需要调整图像大小:

holder.view.ImageView.setImageDrawable(holder.view.ImageView.context.resources.getDrawable(R.drawable.icon))
© www.soinside.com 2019 - 2024. All rights reserved.