每当我向活动中添加ImageView时,我的应用程序中的导航抽屉就会滞后很多。它在没有图像的情况下运行平稳。我猜滞后是因为当我们打开导航抽屉时,活动不断更新。图像分辨率约为1200x800。我尝试使用较低分辨率的图像,但延迟仍然存在。我正在使用Android Studio 1.4 中默认提供的导航抽屉活动
这是我的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();
有没有办法避免这种滞后?
通过TinyPNG.com 压缩您的抽屉
TinyPNG使用智能有损压缩技术来减少PNG文件的文件大小。通过选择性地减少图像中的颜色数量,存储数据所需的字节更少。这种效果几乎是看不见的,但它在文件大小上产生了很大的差异!
这个问题是因为图像视图占用了大量内存。解决方案就是这样!使新文件夹可绘制nodpi。将您的所有资源放到该文件夹中。希望这有帮助:)
让我们让事情变得简单。
从这里将图像转换为SVG格式
然后转到androidstudio,用这个SVG格式创建一个矢量资产。
然后在XML中更改您喜欢的颜色以供背景使用。