我在Android上有一个活动,它真的很慢。它有6个ImageViews,所以我猜是他们造成的。我该如何处理它们呢?我是否要压缩它们,降低它们的质量,以及我如何准确地实现这一点?是不是CardViews导致了运行迟缓?我是否应该压缩来自外部工具的图像,以及我应该将它们设置为多大?另外,有没有内置的函数来实现这一点呢?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#006064"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:id="@+id/ll"
android:weightSum="1"
android:layout_marginTop="-10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clipToPadding="false"
android:onClick="goToChild"
android:gravity="center"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToDash"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="181dp"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="1.02"
android:background="#B2FF59"
android:padding="10dp"
android:src="@drawable/dash2"
android:onClick="goToChild"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="DASHBOARD"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Open the Dashboard"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToSpy"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:id="@+id/routeOption"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1.07"
android:background="#448AFF"
android:padding="10dp"
android:src="@drawable/docico" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="MY DOCUMENTS"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Save documents"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToPlanner"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/plannerOption"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="@+id/imageView5"
android:layout_width="207dp"
android:layout_height="70dp"
android:layout_weight="1.02"
android:background="#F44336"
android:padding="10dp"
android:src="@drawable/plannerico" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="PLANNER"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Set your Plans!"
android:layout_marginBottom="1dp"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToPlanner"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:id="@+id/taskOption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="207dp"
android:layout_height="57dp"
android:layout_weight="1.02"
android:background="#FFEB3B"
android:padding="10dp"
android:src="@drawable/tasks2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="TASKS"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Tasks by Company"
android:layout_marginBottom="1dp"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clipToPadding="false"
android:onClick="goToSummary"
android:gravity="center"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToSummary"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="181dp"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1.02"
android:background="#FFEB3B"
android:padding="10dp"
android:src="@drawable/remico"
android:onClick="goToChild"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="REMARKS"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Company's Remarks"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="goToSettings"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:id="@+id/setting"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1.07"
android:background="#B2FF59"
android:padding="10dp"
android:src="@drawable/meetico" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="MEETING MINUTES"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Meetings Summary"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
发布于 2018-01-11 12:49:20
根据我在你的问题中看到的和提供的信息:
如果图片是资源(放在可绘制的文件夹中),你应该使用足够大的屏幕尺寸和分辨率的phone/tablet/etc.
发布于 2018-01-11 13:13:26
有几种方法可以减少“懒惰”。
发布于 2018-01-11 14:05:34
尝试使用Picasso库:
添加依赖项compile 'com.squareup.picasso:picasso:2.5.2'
ImageView imageView=findViewById(R.id.imageView); Picasso.with(context).load(R.drawable.myimage).fit(). centerCrop().into(imageView);
确保在加载到ImageView之前使用fit()调整图像的大小。否则,如果渲染大量图片,您将消耗额外的内存、遇到滚动缓慢或遇到内存不足问题。
有关毕加索的更多信息,请参阅此link
导致应用程序缓慢的原因是将如此大的图像加载到内存中,因为您使用的是大图像尺寸,毕加索会调整和缓存图像的大小。
https://stackoverflow.com/questions/48207261
复制