【问题标题】:How To Fix Slow Rendering in my Android Code?如何修复我的 Android 代码中的缓慢渲染?
【发布时间】:2018-12-19 10:52:33
【问题描述】:

当我向下滚动时,我的应用程序运行非常缓慢。请帮我解决这个问题。 我之所以担心这一点,是因为这些文章似乎说如果您的应用在 Play 商店排名中排名倒数 25%,Google Play 可能会对其进行处罚。

所有项目资产都存在于此 URL http://garudapixel.com/en/modern-dashboard-ui-design-android-studio-tutorial/

我想以滚动方式制作这种类型的布局...

XML 代码

<?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:layout_height="match_parent"
android:background="@drawable/bgapps"
tools:context=".MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_marginTop="32dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtdashboard"
            android:text="My Dashboard"
            android:textColor="#FFFFFF"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="12 Items"
            android:layout_marginTop="6dp"
            android:layout_below="@+id/txtdashboard"
            android:textColor="#52BAFF"
            android:textSize="14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:layout_alignParentEnd="true"
            android:src="@drawable/person"
            android:layout_width="62dp"
            android:layout_height="62dp" />

    </RelativeLayout>

    <GridLayout
        android:columnCount="2"
        android:rowCount="5"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.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:orientation="vertical"
                android:padding="16dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/bookmark"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

                <TextView
                    android:layout_marginTop="12dp"
                    android:textColor="#6f6f6f"
                    android:textSize="18sp"
                    android:text="Calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </GridLayout>
</LinearLayout>
</ScrollView>

主要活动

package voidgoel.currentaffairs;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

【问题讨论】:

  • 嗯,这很奇怪。图片是不是很大?一种选择可能是尝试将 ListView 与 Adapter 一起使用,但这仍然不能解释为什么会这么慢。
  • 将 RecyclerView 与网格布局管理器一起使用 stackoverflow.com/a/40587169/6772096
  • 使用 recyclerview 和约束布局,而不是嵌套线性和相对布局。
  • 感谢@Zee 的帮助,图像非常大,现在我缩小了尺寸,它正在工作。非常感谢您的帮助

标签: android xml performance android-studio android-layout


【解决方案1】:

就我而言,我遇到了同样的问题。是的,图像非常大。但是您的情况下,您也可以在列表中使用带有YourRecycleView.setLayoutManager(new GridLayoutManager(this,2)); 的回收视图。图像尺寸小对于该模式也非常重要。您可以使用以下链接获取一些指南。

https://www.journaldev.com/13792/android-gridlayoutmanager-example

【讨论】:

  • 感谢您的帮助,图像非常大,现在我减小了尺寸,它正在工作。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2012-01-30
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
相关资源
最近更新 更多