【问题标题】:Android Navigation Drawer Animation Lags/Slows down when an Image View is added over Recycler view当在 Recycler 视图上添加图像视图时,Android 导航抽屉动画滞后/减慢
【发布时间】:2018-07-20 15:40:45
【问题描述】:

我在相对布局中有一个 recyclerview,应用程序运行良好,导航抽屉很流畅。但是我在回收站视图上添加了一个 Imageview,以便我可以根据数据的可用性隐藏和显示。但是一旦我添加了 Imageview,导航抽屉就会变得非常慢。当我删除 Imageview 时,它再次运行顺利。图像大小仅为 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


【解决方案1】:

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

我发现主要问题是因为我的图像文件非常Large,所以 Picasso 将 Full Size 图像加载到 RecycleView 的 ImageView 中,这会导致 Lag

我的解决方案是:(我使用的是 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 代替毕加索

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

holder.view.ImageView.setImageDrawable(holder.view.ImageView.context.resources.getDrawable(R.drawable.icon))

【讨论】:

    【解决方案2】:

    使用 picasso 或 glide 库进行图像加载.. 并在清单中添加一行,您的项目就会像以前一样顺利。

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

    largeHeap 用于避免加载图片时出现内存不足的异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多