【问题标题】:Navigation Drawer Header background image consumes a lot of memoryNavigation Drawer Header 背景图片占用大量内存
【发布时间】:2021-09-13 00:10:47
【问题描述】:

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

问题是,每当我为标题添加背景时,应用程序在刚开始时不加载任何数据的情况下就会将其内存消耗从 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>

my image link

【问题讨论】:

    标签: android navigation-drawer


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

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

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

    【讨论】:

    • 我在帖子中包含的这篇阅读内容来自带有 api 18 的实际三星旧手机,在您回答后我开始进行一些测试,我尝试使用带有 api 30 的实际 OPPO 手机,它使用 95K 无背景和 110K 有背景,我尝试使用 api 18 的虚拟像素 4,它使用 35K 无背景和 54K 有背景
    • 对于 32KB JPEG 照片来说,15MB~25MB 的内存不会太多!!?,如果将来我在应用程序结束时决定使用一些背景不会它消耗越来越多的内存会爆炸,有没有办法避免这种情况?
    • 哦,是的,当我将其调整为 800*450 时,它会将所有设备上的内存消耗降低 10K 到 15K。
    • 希望我的解决方案对您有所帮助。 :) 通过监控多个设备上的内存消耗,您做得很好! (y) ya 15-20 MB 太多了。你可以试试 SVG 看看这是否有助于减少内存消耗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多