【问题标题】:Navigation Drawer lags with an ImageViewNavigation Drawer 滞后于 ImageView
【发布时间】:2019-04-07 16:43:36
【问题描述】:

每当我将 ImageView 添加到我的活动时,我的应用程序中的导航抽屉都会滞后很多。它运行顺利,没有图像。我猜滞后是因为我们打开导航抽屉时活动不断刷新。图像分辨率约为 1200x800。我尝试使用较低分辨率的图像,但延迟仍然存在。我正在使用 Android Studio 1.4 中默认提供的 Navigation Drawer Activity

这是我的 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeScreen"
    tools:showIn="@layout/app_bar_home_screen">



    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="16dp"
        android:text="Replace with home screen"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        andoird:scaleType="centerCrop"
        android:layout_alignTop="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/dinner" />
</RelativeLayout>

这是抽屉布局xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home_screen"
        app:menu="@menu/home_screen_drawer" />

</android.support.v4.widget.DrawerLayout>

这是抽屉布局的java代码

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

有没有办法避免这种延迟?

【问题讨论】:

  • 我试图减小图像的大小(来自here),它对我有用
  • 你能告诉我你把图片缩小到什么尺寸了吗?而且如果它太小了,标签会不会有问题?
  • 大约 400 px...还有一个选项可以保持相同质量的图像...选中该选项不会有任何问题
  • 有一点我忘了说,我是用这张图做背景的,所以400px会不会不好看?

标签: android android-layout


【解决方案1】:

通过TinyPNG.com压缩你的drawables

TinyPNG 使用智能有损压缩技术来减小 PNG 文件的文件大小。通过有选择地减少图像中的颜色数量,存储数据所需的字节数更少。效果几乎不可见,但文件大小有很大差异!

【讨论】:

  • 我认为您的回答很好,但并不完整。我有一些滞后,即使压缩我的图像。但是很棒的网站,谢谢!
  • 好的,我的图片太大了(比如 1800x1200),现在已经很流畅了 =D 谢谢
【解决方案2】:

这是个问题,因为 imageview 占用了很多内存。 解决方案是这样的! 制作新文件夹drawable-nodpi。 把你所有的资源放到那个文件夹里。 希望这有帮助:)

【讨论】:

    【解决方案3】:

    让我们把事情简单化。

    将图片从here转换成SVG格式

    然后去 android studio 用这个 SVG 格式创建一个矢量资源。

    然后在 XML 中更改您喜欢的颜色以供背景使用。

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多