首页 > 解决方案 > Navigation Drawer Header 背景图片占用大量内存

问题描述

所以今天我开始配置我的导航抽屉,并从制作标题开始。

问题是,每当我为标题添加背景时,应用程序在刚开始时不加载任何数据的情况下就会将其内存消耗从 10,000K~12000K 提高到 35000K~40000K,我实际上是在使用一个空片段。

所以很自然,当我开始带来一些照片的数据时(我的应用程序有一部分通过 Glide 的回收器视图显示来自外部存储的照片)应用程序变得疯狂并且 Glide 由于内存不足而无法加载任何内容(应用程序滚动到回收站视图后跳过 64000K)

我想避免使用大堆选项,这就是为什么我想知道为什么标题背景消耗所有这些兆字节,尽管它的大小仅为 32KB,JPEG 类型,它的分辨率是 1600X900 ?以及如何避免这种内存扩展(如何将图像正确地作为背景/可绘制)?

我的导航抽屉标题代码:

    <androidx.constraintlayout.widget.ConstraintLayout 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="200dp"
    android:background="@drawable/navigation_header_bg">

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="10dp"
        android:contentDescription="@string/image_basic_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize50Percent"
        app:srcCompat="@drawable/ic_person"
        android:scaleType="centerCrop"
        />

    <TextView
        android:id="@+id/username_textview_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:text="@string/not_logged_in_header"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@id/imageView"
        app:layout_constraintTop_toTopOf="@id/imageView"
        app:layout_constraintVertical_bias="0.29000002" />

    <TextView
        android:id="@+id/email_textview_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:text="@string/no_email_header"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintStart_toEndOf="@id/imageView"
        app:layout_constraintTop_toBottomOf="@id/username_textview_header"
        app:layout_constraintVertical_bias="0.14999998" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的图片链接

图像属性

标签: androidnavigation-drawer

解决方案


  1. 它不仅仅是图像的大小。在 Android 中,图像的分辨率会影响视图的性能,这对于某些 android 设备可能不是问题,但在其他设备中会导致问题。尝试使用Paint(Windows)或合适的图像查看器(在其他平台上)通过裁剪将图像的宽度和高度减小到800。

  2. 然后在您的设备上重新运行该应用程序并检查

  3. 此外,如果可能,请分享您的设备名称和型号以及 Android 版本。


推荐阅读