Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer lags with an ImageView

Whenever I add an ImageView to my activity, the navigation drawer in my app lags a lot. 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. I tried using a lower res image, but the lag still persists. I am using the Navigation Drawer Activity that comes by default in Android Studio 1.4

This is my xml code:

<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 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

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?

like image 549
ichigo gyuunyuu Avatar asked Nov 14 '15 02:11

ichigo gyuunyuu


2 Answers

Compress your drawables by TinyPNG.com

TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. 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!

like image 124
Mr Robot Avatar answered Oct 10 '22 11:10

Mr Robot


That's problem is beacuse imageview taking a lot memory. the solution is this ! make new folder drawable-nodpi. put your all resources to that folder. hope this help :)

like image 42
San Lopess Avatar answered Oct 10 '22 09:10

San Lopess