简体   繁体   English

导航抽屉滞后于ImageView

[英]Navigation Drawer lags with an ImageView

Whenever I add an ImageView to my activity, the navigation drawer in my app lags a lot. 每当我向我的活动添加ImageView时,我的应用程序中的导航抽屉都会滞后。 It runs smoothly without the image. 没有图像,它运行顺畅。 I am guessing the lag is because the activity keeps getting refreshed as we open the nav drawer. 我猜测滞后是因为我们打开导航抽屉时活动不断刷新。 The image resolution is around 1200x800. 图像分辨率约为1200x800。 I tried using a lower res image, but the lag still persists. 我尝试使用较低的res图像,但滞后仍然存在。 I am using the Navigation Drawer Activity that comes by default in Android Studio 1.4 我正在使用Android Studio 1.4中默认提供的导航抽屉活动

This is my xml code: 这是我的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>

This is the drawer layout xml code: 这是抽屉布局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>

And this is the java code for the drawer layout 这是抽屉布局的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();

Is there a way to avoid this lag? 有没有办法避免这种滞后?

Compress your drawables by TinyPNG.com 通过TinyPNG.com压缩您的drawables

TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. TinyPNG使用智能有损压缩技术来减少PNG文件的文件大小。 By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. 通过选择性地减少图像中的颜色数量,存储数据所需的字节更少。 The effect is nearly invisible but it makes a very large difference in file size! 效果几乎是不可见的,但它在文件大小上产生了很大的差异!

That's problem is beacuse imageview taking a lot memory. 这个问题是因为imageview占用了大量内存。 the solution is this ! 解决方案是这个! make new folder drawable-nodpi. 制作新的文件夹drawable-nodpi。 put your all resources to that folder. 将您的所有资源放到该文件夹​​中。 hope this help :) 希望这个帮助:)

Let us make things simple. 让我们简单一点。

Convert Image into SVG format from here 这里将图像转换为SVG格式

Then go to android studio and create a vector asset from this SVG format. 然后转到android studio并从此SVG格式创建矢量资产。

Then change the colors that you like in XML for background usage. 然后在XML中更改您喜欢的颜色以供背景使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM