#android #xml #android-studio #layout
#Android #xml #android-studio #макет
Вопрос:
У меня есть дизайн макета в моем приложении.
Он работает плавно на эмуляторе, но когда я открываю приложение на реальном устройстве и прокручиваю его, оно прокручивается грубо, я имею в виду, не плавно, слишком медленно.
(Я использовал android: largeHeap =»true», иначе он не работал на моем устройстве, а только на эмуляторе.)
Можете ли вы помочь мне сделать его плавным?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<GridLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FirstFragment"
android:columnOrderPreserved="false"
android:alignmentMode="alignMargins"
android:rowCount="6"
android:columnCount="2">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/animal_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/elephant"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animals"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/art_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/art"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Art"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/biography_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/biography"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Biography"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/countries_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/countries"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Countries"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/culture_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/culture"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Culture"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/environment_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/environment"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Environment"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/health_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/health"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Health"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/holidays_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/holidays"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Holidays"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/literature_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/literature"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Literature"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/politics_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/politics"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Politics"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/science_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/science"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Science"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp"
>
<LinearLayout
android:id="@ id/sports_layout"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/sport"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"
android:textSize="18sp"
android:textColor="#6f6f6f"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</ScrollView>
Ответ №1:
Я решил проблему, уменьшив размеры изображений в ImageView.
Ответ №2:
Каждый виджет и макет, которые вы добавляете в свое приложение, требуют инициализации, компоновки и рисования. Например, использование вложенных экземпляров LinearLayout может привести к чрезмерно глубокой иерархии представлений. Кроме того, вложение нескольких экземпляров LinearLayout, использующих параметр layout_weight, может быть особенно дорогостоящим, поскольку каждый дочерний элемент необходимо измерять дважды. Это особенно важно, когда макет многократно раздувается, например, при использовании в ListView или GridView.
Читайте здесь для оптимизации макета — https://developer.android.com/training/improving-layouts/optimizing-layout
Решение —
ConstraintLayout — это новый способ достижения той же плоской структуры xml. Это, несомненно, улучшит ваше время отклика
Подробнее о том, как компоновка ограничений повышает производительность, можно прочитать на
https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html
Комментарии:
1. Спасибо вам за ваш ответ. Я использовал компоновку ограничений с рекомендациями в своем предыдущем проекте, и я могу сказать, что использование компоновки ограничений было очень простым и удобным для памяти. В этом случае уменьшение размера изображения сработало, но ваше мнение верно.
2. Спасибо, я надеюсь, что это поможет максимально использовать svg вместо png, и если вы сочтете это полезным, не могли бы вы отметить этот ответ правильным или 1
3. в нем говорится: «Спасибо за отзыв! Голоса, поданные теми, у кого репутация менее 15, записываются, но не изменяют публично отображаемую оценку публикации.» когда моя учетная запись активируется, я дам вам очко. Снова Ty.