Android 我的活动如何在安卓系统中顺利运行,并有大量高质量的图像?

Android 我的活动如何在安卓系统中顺利运行,并有大量高质量的图像?,android,Android,场景:我的Android应用程序中有两个活动:活动X和活动Y 在活动X上,有一个按钮,点击按钮,我打开活动Y 在活动Y的OnCreate中,我正在调用Firestore数据库,并获取大约10个图像链接。该图像链接是动态的,而且该链接是Firebase存储链接,实际的高质量图像上传到该链接 在获取图像链接后,我使用Glide库设置ImageView 问题: 显然,为了显示10个大图像,我在ActivityXML中添加了Scrollview,以便用户可以滚动和查看图像。但滚动行为并不顺利。这也不仅仅

场景:我的Android应用程序中有两个活动:活动X和活动Y

在活动X上,有一个按钮,点击按钮,我打开活动Y

在活动Y的OnCreate中,我正在调用Firestore数据库,并获取大约10个图像链接。该图像链接是动态的,而且该链接是Firebase存储链接,实际的高质量图像上传到该链接

在获取图像链接后,我使用Glide库设置ImageView

问题: 显然,为了显示10个大图像,我在ActivityXML中添加了Scrollview,以便用户可以滚动和查看图像。但滚动行为并不顺利。这也不仅仅是第一次出现问题。在将所有图像加载到Imageview之后,如果我尝试滚动,则滚动也不平滑

代码:

String str = entry.getValue().toString();

switch (entry.getKey()) {
            case "link1":
                setImage(binding.image1, str);
                break;

            case "link2":
                setImage(binding.image2, str);
                break;

            case "link3":
                setImage(binding.image3, str);
                break;

            case "link4":
                setImage(binding.image4, str);
                break;

            case "link5":
                setImage(binding.image5, str);
                break;
        }
    }


     private void setImage(ImageView image1, String str) {
            image1.setVisibility(View.VISIBLE);
            Glide.with(context).load(str).
                    diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(image1);
        }
<androidx.recyclerview.widget.RecyclerView xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/app_bar"
        android:clipToPadding="false"
        android:fadingEdge="none"
        android:fastScrollEnabled="true"
        android:focusable="true"
        android:overScrollMode="never"
        tools:listitem="@layout/row_preview_view" />
更新:

String str = entry.getValue().toString();

switch (entry.getKey()) {
            case "link1":
                setImage(binding.image1, str);
                break;

            case "link2":
                setImage(binding.image2, str);
                break;

            case "link3":
                setImage(binding.image3, str);
                break;

            case "link4":
                setImage(binding.image4, str);
                break;

            case "link5":
                setImage(binding.image5, str);
                break;
        }
    }


     private void setImage(ImageView image1, String str) {
            image1.setVisibility(View.VISIBLE);
            Glide.with(context).load(str).
                    diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(image1);
        }
<androidx.recyclerview.widget.RecyclerView xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/app_bar"
        android:clipToPadding="false"
        android:fadingEdge="none"
        android:fastScrollEnabled="true"
        android:focusable="true"
        android:overScrollMode="never"
        tools:listitem="@layout/row_preview_view" />
尝试使用RecyclServiceView而不是Scrollview,但回收器是否可能更平滑。我尝试了以下代码

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
                    @Override
                    protected int getVerticalSnapPreference() {
                        return LinearSmoothScroller.SNAP_TO_ANY;
                    }
                };
                smoothScroller.setTargetPosition(0);
                mLayoutManager.startSmoothScroll(smoothScroller);
XML:

String str = entry.getValue().toString();

switch (entry.getKey()) {
            case "link1":
                setImage(binding.image1, str);
                break;

            case "link2":
                setImage(binding.image2, str);
                break;

            case "link3":
                setImage(binding.image3, str);
                break;

            case "link4":
                setImage(binding.image4, str);
                break;

            case "link5":
                setImage(binding.image5, str);
                break;
        }
    }


     private void setImage(ImageView image1, String str) {
            image1.setVisibility(View.VISIBLE);
            Glide.with(context).load(str).
                    diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(image1);
        }
<androidx.recyclerview.widget.RecyclerView xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/app_bar"
        android:clipToPadding="false"
        android:fadingEdge="none"
        android:fastScrollEnabled="true"
        android:focusable="true"
        android:overScrollMode="never"
        tools:listitem="@layout/row_preview_view" />


还有,是否有任何缓存机制或其他可能的机制?我正在使用Glide库加载图像。我可以用Glide做些什么吗?

也许可以尝试用
回收器视图替换
滚动视图
。在android文档中,我们可以读到“如果你的应用程序需要显示基于大数据集(或经常更改的数据)的元素滚动列表,你应该使用RecyclerView”。我不确定,但我认为使用
RecyclerView
会更流畅。

是的,
RecyclerView
对性能进行了重大改进,尤其是在显示图像时。线索是
RecyclerView
在你向下或向上滚动时不会加载新的元素和图像,而是在你从视线中滚动到顶部或底部的
视图中循环使用,并将其附加到你滚动的方向。感谢它的工作,但我需要更平滑的操作。请检查我的最新答案。@suchising如果你还没有这样做,你可以说我不明白:一切都很好,但有一件事我不明白。根据建议,我使用了Recyclerview,而不是Scrollview。但现在第一个视图设置在中间的某个地方。我无法维持该职位。您显示使用
RecyclerView
的更新看起来不正确。请参阅本指南,了解如何使用
RecyclerView
:。此外,如果你在互联网上做任何事情,不缓存是不明智的。为了正确缓存,我建议将图片保存到设备上的文件中,然后将它们的路径存储在
房间
数据库中,并在需要时加载它们<代码>房间
:.@René也会用房间来监视,速度会很慢,因为APK MB会很高。没有人会下载appEveryone在他们看到网络数据使用量大幅飙升后会卸载该应用程序,因为每次启动应用程序时,他们都会下载多MB的图片。对于某些用户来说,这甚至可能会产生高昂的成本。@Rene Spies根据您的建议,即使我将存储到SQLite中,APK大小也将是76 MB,因为大约有100个图像,您无法神奇地删除图像的大小。通常的任务方法如下。您在线存储图像,因此APK的初始大小不会因此而变大。您可以让用户决定是否下载每个图像,以便用户可以控制该图像。您可以下载图像并将其存储在手机的本地驱动器上,并在数据库中存储对此路径的引用。建议执行最后一个步骤,因为在数据库中存储大型对象将导致严重的性能缺陷。